Hi guys,
I am having problems with a simple program I wrote, here's the source code:

//seventh
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Maths
{
public:
void enterInstructions( int instructions )
	{
	cout <<"I want to do a: \nAddition\nSubtraction\nType your choice here: "<< instructions << endl;
	}
void conditions(int instructions)
	{
	int result;
	int Addition;
	int Subtraction;
	if ( instructions == Addition )
	cout << "Enter the right number: 9+3= ";
	cin >> result;
	if ( result == 12 )
	cout << "Well done, you're a star!";
	if ( result != 12 )
	cout << "Sorry, wrong answer!";
	if ( result < 12 )
	cout << "Sorry, wrong answer!";
	if ( result > 12 )
	cout << "Sorry, wrong answer!";

	if ( instructions == Subtraction )
	cout << "Enter the right number: 9-3= ";
	cin >> result;
	if ( result == 6 )
	cout << "Well done, you're a star!";
	if ( result != 6 )
	cout << "Sorry, wrong answer!";
	if ( result < 6 )
	cout << "Sorry, wrong answer!";
	if ( result > 6 )
	cout << "Sorry, wrong answer!";
	}
};

int main()
{
int myInstructions;
Maths operations;
cout << "What do you want to do?\n";
cin >> myInstructions;
operations.enterInstructions( myInstructions );
operations.conditions( myInstructions );


return 0;
}

The program doesn't execute properly. When I launch the first thing that I see on the screen is the "What do you want to do?" message and then I have to input a character or a number in order to have the next message "I want to do a :" to come up. The thing is that when I type something - in this case the letter d - what I get is the following:


What do you want to do?

d
I want to do a:
Addition
Subtraction
Type your choice here: -858993460
Enter the right number: 9+3= Sorry, wrong answer!Sorry, wrong answer!Enter the r
ight number: 9-3= Sorry, wrong answer!Sorry, wrong answer!Press any key to conti
nue

I am not sure what I have done wrong, I looked at the code again and again, but I am not sure what I can do...
thanks

When you enter your number, the cin reads the number. But you also pressed ENTER. That's left in the input buffer for the next cin . Look up the .ignore() method. That will clear the buffer.

Ok, I used the cin.ignore ( 80, '\n' ); in the code,but also I think I noticed something wrong in main and now I moved cin one line down so that the source code is now:

//seventh
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

class Maths
{
public:
void enterInstructions( int instructions )
	{
	cout <<"I want to do a: \nAddition\nSubtraction\nType your choice here: "<< instructions << endl;
	}
void conditions(int instructions)
	{
	int result;
	int Addition;
	int Subtraction;
	if ( instructions == Addition )
	cout << "Enter the right number: 9+3= " << endl;
	cin >> result;
	if ( result == 12 )
	cout << "Well done, you're a star!" << endl;
	if ( result != 12 )
	cout << "Sorry, wrong answer!" << endl;
	if ( result < 12 )
	cout << "Sorry, wrong answer!" << endl;
	if ( result > 12 )
	cout << "Sorry, wrong answer!" << endl;

	if ( instructions == Subtraction )
	cout << "Enter the right number: 9-3= " << endl;
	cin.ignore ( 80, '\n' ); 
	cin >> result;
	if ( result == 6 )
	cout << "Well done, you're a star!" << endl;
	if ( result != 6 )
	cout << "Sorry, wrong answer!" << endl;
	if ( result < 6 )
	cout << "Sorry, wrong answer!" << endl;
	if ( result > 6 )
	cout << "Sorry, wrong answer!" << endl;
	}
};

int main()
{
int myInstructions;
Maths operations;
cout << "What do you want to do?\n";

operations.enterInstructions( myInstructions );

cin >> myInstructions;
operations.conditions( myInstructions );


return 0;
}

I put the cin.ignore line only once just before the second cin, but nothing much has changed, the program doesn't work but when I execute it from the compiler, the first thing that I see is (you can also see this in the attached screenshot "number1"):

What do you want to do?
I want to do a:
Addition
Subtraction
Type your choice here: -858993460


The I type Addition and it comes up with this ( you can see this in the attached screenshot "number 2")

What do you want to do?
I want to do a:
Addition
Subtraction
Type your choice here: -858993460
Addition
Enter the right number: 9+3=
Sorry, wrong answer!
Sorry, wrong answer!
Enter the right number: 9-3=
Sorry, wrong answer!
Sorry, wrong answer!
Press any key to continue

So basically it doesn't give me the chance to imput the numbers...

Uhm, maybe the problem is with the variables I used...maybe I got things wrong with this program. All I wanted do do was for a user to be able to make a choice between addition and subtraction, and the program to be able to present him with respectively "Enter the right number: 9+3= " or "Enter the right number: 9-3= " depending on user's choice...did I choose the wrong code then? Sorry I am not quite sure....

There's something I don't understand in your code:

cin >> result;
	if ( result == 12 )
	cout << "Well done, you're a star!" << endl;
        // why not use an ELSE here because if the above is false
	if ( result != 12 )  // it's obvious result is != 12
	cout << "Sorry, wrong answer!" << endl;
	if ( result < 12 )  // it's also obvious result is < 12 or ...
	cout << "Sorry, wrong answer!" << endl;
	if ( result > 12 )  // ... > 12 because it's !=
	cout << "Sorry, wrong answer!" << endl;

Hi there, thanks for that. Well, the reasons why I haven't used ELSE statement because I haven't heard of them untill now, I haven't got that far yet. I compiled my program again, and I got few warnings:
--------------------Configuration: 10 - Win32 Debug--------------------
Compiling...
10th.cpp
Z:\example\10\10th.cpp(53) : warning C4700: local variable 'myInstructions' used without having been initialized
Z:\example\10\10th.cpp(19) : warning C4700: local variable 'Addition' used without having been initialized
Z:\example\10\10th.cpp(31) : warning C4700: local variable 'Subtraction' used without having been initialized
Linking...

10.exe - 0 error(s), 3 warning(s)

I thought to myself, well thery are just warnings so nothing to worry about, but now thinking about that again, maybe I should take them more seriously. I guess I should focus a bit more on the theory then...

you need to set the variable "result" to the answer otherwise its using a random number eg, -858993460

//seventh

#include <iostream>

using std::cout;
using std::cin;
using std::endl; 

class Maths{

public:void enterInstructions( int instructions )

	{	cout <<"I want to do a: \nAddition\nSubtraction\nType your choice here: "<< instructions << endl;	}

void conditions(int instructions)	
{	int result = 12;
	int Addition;
	int Subtraction;

	if ( instructions == Addition )	

cout << "Enter the right number: 9+3= " << endl;

	cin >> result;	

if ( result == 12 )	
      cout << "Well done, you're a star!" << endl;	
if ( result != 12 )	
     cout << "Sorry, wrong answer!" << endl;	
if ( result < 12 )	
     cout << "Sorry, wrong answer!" << endl;	
if ( result > 12 )	
     cout << "Sorry, wrong answer!" << endl;

im sure this will solve your problem as im new to c++ myself

warning C4700: local variable 'myInstructions' used without having been initialized

Consider these warnings as errors actually. If you don't fix these warnings (all of them), you will be in for surprises. And in general, pay attention to the compiler warnings, and don't ignore them.

I added 12 to the result so that I have

int result = 12;

but it didn't help, because I seem to understand that all my variables need to be initialized taking into consideration the compiler warnings. I didn't know that variables in general had to be initialised, I thought that was optional. The thing is I am not sure what values my variables should get (apart from the result of course) but nevermind, I will go on with the theory a bit more and have a look at the "if conditions" with the "ELSE statement" as suggested by WaltP.
thanks

I added 12 to the result so that I have

int result = 12;

but it didn't help, because I seem to understand that all my variables need to be initialized taking into consideration the compiler warnings. I didn't know that variables in general had to be initialised, I thought that was optional. The thing is I am not sure what values my variables should get (apart from the result of course) but nevermind, I will go on with the theory a bit more and have a look at the "if conditions" with the "ELSE statement" as suggested by WaltP.
thanks

it is good practice to initalize your variables so you dont have random numbers, thinking over your code what i said dont listen lol,
if you want some good tutorial use 3d buzz and with your if statments your wasting time writing to much

if ( result == 12 )	      
                    cout << "Well done, you're a star!" << endl;	
if ( result != 12 )
	     cout << "Sorry, wrong answer!" << endl;	
if ( result < 12 )
	     cout << "Sorry, wrong answer!" << endl;	
if ( result > 12 )
	     cout << "Sorry, wrong answer!" << endl;

Try

if ( result == 12 )
	      cout << "Well done, you're a star!" << endl;	
if ( result != 12 )
	     cout << "Sorry, wrong answer!" << endl;

With your code your saying if result is equal to 12 say "well done youre a star"
if its not equal to 12 say "sorry wrong answer",
if its more then 12 say "sorry wrong answer"
if its less then 12 say "sorry wrong answer",

You only need the equal to and not equal to

I see, yes I haven't realized that I had superfluos conditions, I should be thinking a bit more.
Thanks for your help

i have made a simple calculator with c++

#include <iostream>

using namespace std;


int main ()
{
loop:
	int main1 = 0;
	float A = 0;
	float B = 0;
	float total1 = 0;
	float total2 = 0;
	float total3 = 0;
	float total4 = 0;

	cout << endl;
	cout << "What do you want to do? " << endl;
	cout << "press 1 for addition " << endl;
	cout << "press 2 for subtraction " << endl;
	cout << "press 3 for multiplation " << endl;
	cout << "press 4 for division " << endl;
	cin >> main1;

	if (main1 == 1)
	{
	cout << "enter first number for the addition ";
	cin >> A;
	
	cout << "enter second number for addition ";
	cin >> B;
	cout << endl;
		total1 = A + B;
		cout << A << " + " << B << " = " << total1 ;
	}
	if (main1 == 2)
	{
		cout << "enter first number for the subtraction ";
	cin >> A;
	
	cout << "enter second number for subtration ";
	cin >> B;
	cout << endl;
		total2 = A - B;
		cout << A << " - " << B << " = " << total2 ;
	}
	if (main1 == 3)
	{
		cout << "enter first number for the multiplaction ";
	cin >> A;
	cout << "enter second number for multiplation ";
	cin >> B;
	cout << endl;
		total3 = A * B;
		cout << A << " x " << B << " = " << total3 ;
	}

	if (main1 == 4)
	{
		cout << "enter first number for the division ";
	cin >> A;
	cout << "enter second number for division ";
	cin >> B;
	cout << endl;
		total4 = A / B;
		cout << A << " / " << B << " = " << total4 ;
	}
	goto loop;

can you see how i have initalized my variables before using them,
i no there is a easier way to do this, but i havent got to that part of the tutorial at the moment

hi nats01282,
thanks for posting that, it is actually really helpful because it shows exaclty how to initialize the variables. I tried myself with my program (to initialize the variables few days ago), and I can see why I failed, because the structure of my program was completely wrong, it didn't make any sense.

Looking at your program, there are few things I haven't touched yet, like the goto statements but I get the general idea, so thanks. SO far I got to the while loop, but I will do some more today.

Looking at your program, there are few things I haven't touched yet, like the goto statements but I get the general idea, so thanks.

Good. Stay away from the goto statement. It's considered bad programming and almost always an unnecessary command in C++.

To expand upon what nat had showed you, I would also like to introduce you to 'loops' and 'switch statements'. They are not hard to learn, and are essential when making a program. Using the 'goto' statement is not a good way to integrate a loop. Avoid it at all costs.

So I basically made the same program but more efficient. To use it just enter in commands such as:

2 * 2
4 + 7
5 - 9
6 / 2
etc.

Here it is, and I hope it helps:

#include<iostream>
using namespace std;
int main()
{
	for(int x=0;x<5;x++) //tells the program to run 5 times
	{
		float num1, num2, total;
		char operation;
		cout << "Please enter your equation: "; //prompts the user
		cin >> num1 >> operation >> num2;//inputs the 3 variables into the program
		switch(operation)//changes the 'second variable' or 'operation' depending on the switch case
		{
		case 'x':
		case '*':				//multiplication
			total = num1*num2;
			break;
		case '/':				//division
			total = num1/num2;
			break;
		case '-':				//subtraction
			total = num1-num2;
			break;
		case '+':				//addition
			total = num1+num2;
			break;
		}
		cout << num1 << " " << operation << " " << num2 << " = " << total << endl << endl; //displays result
	}
}

To expand upon what nat had showed you, I would also like to introduce you to 'loops' and 'switch statements'. They are not hard to learn, and are essential when making a program. Using the 'goto' statement is not a good way to integrate a loop. Avoid it at all costs.

So I basically made the same program but more efficient. To use it just enter in commands such as:

2 * 2
4 + 7
5 - 9
6 / 2
etc.

Here it is, and I hope it helps:

#include<iostream>
using namespace std;
int main()
{
	for(int x=0;x<5;x++) //tells the program to run 5 times
	{
		float num1, num2, total;
		char operation;
		cout << "Please enter your equation: "; //prompts the user
		cin >> num1 >> operation >> num2;//inputs the 3 variables into the program
		switch(operation)//changes the 'second variable' or 'operation' depending on the switch case
		{
		case 'x':
		case '*':				//multiplication
			total = num1*num2;
			break;
		case '/':				//division
			total = num1/num2;
			break;
		case '-':				//subtraction
			total = num1-num2;
			break;
		case '+':				//addition
			total = num1+num2;
			break;
		}
		cout << num1 << " " << operation << " " << num2 << " = " << total << endl << endl; //displays result
	}
}

I used the goto loop and added a option for exiting the program, the program doeas a constant loop until the user tells the program to exit,
and as a beginer may i just ask why a goto loop is so bad? and not used that much?

I used the goto loop and added a option for exiting the program, the program doeas a constant loop until the user tells the program to exit,
and as a beginer may i just ask why a goto loop is so bad? and not used that much?

Well a goto loop is not an actual error, but more of an inconvenience. You see, when you use a 'goto' it changes the format of your code and makes it harder for other coders to read. This discourages people to review your code as they may have to reformat the whole thing to get an adequate understand of where your loops end and such.
So for small programs it won't do much harm, but for larger programs it can be a pain in the neck. Therefore it is just good practice to never use them.

thanks guys, I will bear in mind not to use the goto.

I do not understand your code... First you should have just used

using namespace std;

instead of waht you did, you also did not initialize your variables,and then you kept repeating the same conditions:

if(result==12)//OK
 if(result!=12)/*If the first staement didn't execute then surely you don't need this, an else would do the trick!*/
if(result<12)/*The statement you wanted to be printed out just once would reprint again, cos result may meet this condition too, the more reason this isn't needed*/
 if(result>12)//Same as above

I do not understand your code... First you should have just used

using namespace std;

instead of waht you did, you also did not initialize your variables,and then you kept repeating the same conditions:

using std::cout;
using std::cin;
using std::endl;

is actually perfectly acceptable and in many situations preferred to bringing all of the std:: methods into the current namespace. The way it was written the only methods that can be called unqualified are those 3.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.