Simple C++ question Someone give me some help on this

Curt

apostate
Joined
Feb 2, 2004
Posts
7,710
I've written a simple 4 function calculator for my Computer Science course, and it's pretty ok. But one of the requirements is that it shouldn't crash if a user inputs a character or word instead of an integer.

here's a piece of the code for division prototype: ( I figured out how to not crash if it divides by 0...pretty easy)





Code:
void Division (void)

 {
  float divint1,divint2,divans;
		cout<<"What is the dividend";
		cin >>divint1;
		cout<<"What is the divisor?";
		cin >>divint2;
             if (divint2==0)
			{
				cout<<"Do not divide by zero you dolt!";
            }

			divans=divint1/divint2;

			if (divint1 < divint2)
			{
				cout<<"Your first number must be larger than your second.";
			}

                cout<<"\n"<<endl;
                cout<<divint1<<"/"<<divint2<<"="<<divans<<"."<<endl;
                cout<<"\n"<<endl;
   }

I know I need to do either a if statement, a while loop, or even a throw/catch, right after the cin>> of a character... but how do I tell it that?

Any help would be greatly appreciated.

Here's the .cpp file
http://userwww.sfsu.edu/~curtwrx/csc205/STAYONTARGET.cpp
and the compiled exe
http://userwww.sfsu.edu/~curtwrx/csc205/STAYONTARGET.exe
 
Last edited:

Curt

apostate
Joined
Feb 2, 2004
Posts
7,710
kafuin_gaira said:
do your own fucking homework, princess

Nice +1 kafuin_gaira

I'm just turning it in as is... I just needed some help with one line of code.... Perhaps Kernow. :p
 

kobylka68

Basara's Blade Keeper
Joined
Jul 12, 2003
Posts
3,666
I'm pretty sure C++ has a isnumneric or isinteger function somewhere in there. I haven't used C++ in a while but I could have sworn it was there.
 

neo>all

Super nO0b
Joined
Jan 21, 2005
Posts
4,339
I took a programming class and i remeber racking my brains out right before a deadline only to come up with the answer the day after it's due. Your code looks good but i can't help you since that would be cheating. :kekeke:

PS: I don't know the answer anyway
 

Tacitus

Volatile Memory Construct - SN://0467839
Staff member
Joined
Apr 26, 2002
Posts
15,120
IF i were to give you the answer would that NOT defeat the purpose of your course? there's a certain STATEMENT of CHARACTER that comes with getting YOUR ANSWER yourself.

..just saying..
 

RiotoftheBlood

Chin's Drinking Partner
Joined
Jan 10, 2001
Posts
2,779
Nicely said.

I'm sure you have a book for the class. Why don't you look in there?

VanillaThunder said:
IF i were to give you the answer would that NOT defeat the purpose of your course? there's a certain STATEMENT of CHARACTER that comes with getting YOUR ANSWER yourself.

..just saying..
 

Hildegarn326

Benimaru's Hairdresser
Joined
Jul 26, 2002
Posts
780
Um, I don't think you have to use an if statement after using the cin>> operator, just if you feel like you want to. And if you want to, you should already know how, based on the fact that you already used one in your code. Why would you NEED an if statement after using cin>>?
 

Hildegarn326

Benimaru's Hairdresser
Joined
Jul 26, 2002
Posts
780
Code:
void Division (void)

 {
  float divint1,divint2,divans;
                
		cout<<"What is the dividend";
		cin >>divint1;
		cout<<"What is the divisor?";
		cin >>divint2;
             if (divint2==0)
			{
				cout<<"Do not divide by zero you dolt!";
                                [b]break;[/b]
            }
              [b]if(divint2!=0)
              {[/b]

			divans=divint1/divint2;

			if (divint1 < divint2)
			{
				cout<<"Your first number must be larger than your second.";
			}

                cout<<"\n"<<endl;
                cout<<divint1<<"/"<<divint2<<"="<<divans<<"."<<endl;
                cout<<"\n"<<endl;
               [b]break;
              }[/b]
   }
This is just me half-assing it after not coding for a LONG time.
 

kernow

Superior Being
20 Year Member
Joined
Sep 1, 2001
Posts
37,322
interesting..

I haven't done cpp in a long time, but why does your proggy not quit when you press q then enter?

compiles and works fine on my linux machine, didn't try the windows executable.

I am guessing you need some sort of input validation or exception handling, but then, you could use a class for 'easy data entry' that has functions like readInt() readChar() from standard in, and then give you return codes based on failure or success

I'm sure some input classes like this exist.

Linux binary here.
 

rarehero

Rotterdam Nation Resident,
20 Year Member
Joined
Jan 12, 2001
Posts
13,428
kafuin_gaira said:
do your own fucking homework, princess

awesome.
i remember having a class in c and trying to find help on the internet
and finding people saying the same thing on other forums.
this is the place i like going, because there seems to be a good amount
of experts here
http://forums.devshed.com/
but like any other place, don't expect anyone to just give you an answer.
 

Curt

apostate
Joined
Feb 2, 2004
Posts
7,710
I actually forgot about this for a few days. I ended up submitting it as is.

Thanks everyone for your input, i'll use your advice in my next project.

Neo-Geo.com is the best.
 
Top