Angry"Skip" Problem? [Solved]

Posts 1–14 of 14 · Page 1 of 1
"Skip" Problem? [Solved]
So I just started learning C++, and I figured the best way to remember / figure out new things is to practice. So, I was going to build a simple calculator. But I have a problem >_< here's the code so far. And I know it is un-organized, but I was trying to remove spaces and do stupid things to desperately make it work..

(ONLY _ADD() IS FINISHED)
Code:
#include "stdafx.h"
#include <iostream>

using namespace std;

//Global Variables

	int x;
	int y;
	int userAnswerX;
	int userAnswerY;

	int userChoice;

//Global Variables

int main()
{
	cout << "===================\n";
	cout << "=    Calculator   =\n";
	cout << "===================\n";
	cout << "1) Add\n";
	cout << "2) Subtract\n";
	cout << "3) Multiply\n";
	cout << "4) Divide\n";
	cout << "(Type the number)\n";
	
	cin >> userChoice;

		if (userChoice==1) _add();

			if (userChoice==2) _subtract();

				if (userChoice==3) _multiply();

					if (userChoice==4) _divide();
						
						else (system("pause>nul"));

}


int _add ()
{
	cout << "\nEnter value for X: ";

	cin >> userAnswerX;

	x = userAnswerX;

	cout << "\nEnter value for Y: ";
	
	cin >> userAnswerY;

	y = userAnswerY;

	cout << "\nYou set X equal to ";
	cout << x;
	cout << "\nYou set Y equal to ";
	cout << y;
	cout << "\n \n";
	
	cout << x + y
	
	system("pause>nul");
}

int _subtract ()
{

}

int _multiply ()
{

}

int _divide ()
{

}
Here is a SS of what happens when I first build / run:



Here's what SHOULD happen:

Code:
================
=  Calculator  =
================
1) Add
2) Subtract
3) Multiply
4) Divide
(Type the number)

"User types number here"
( Goes to _add(), _subtract(), _multiply(), or _divide() )
Sorry if I wasn't perfectly clear, feel free to ask questions.

Edit: If you can't see the screen shot, download the attachment.
C++_SS.jpgattachment deleted · 2 downloads before removal
Don't post the screenshot as an attachment.. post it like this [img]Your screenshot link[/img] When I see the picture I can assist you a bit more.
Are you saying that when you run your calculator it doesn't skip lines ?
Quote Originally Posted by Terell. View Post
Don't post the screenshot as an attachment.. post it like this [img]Your screenshot link[/img] When I see the picture I can assist you a bit more
I did, it doesn't seem to be working...can I just [URL] it?
Photo here

Quote Originally Posted by Terell. View Post
Don't post the screenshot as an attachment.. post it like this [img]Your screenshot link[/img] When I see the picture I can assist you a bit more.
Are you saying that when you run your calculator it doesn't skip lines ?
Sorry, didn't realize what else you said. The problem is it IS skipping lines. It skipped everything after ==== Calculator ==== and went strait to _add()

Photo Here
Sure that'll work.
So you want

Code:
	cout << "===================\n";
	cout << "1) Add\n";
	cout << "2) Subtract\n";
	cout << "3) Multiply\n";
	cout << "4) Divide\n";
	cout << "(Type the number)\n";
to appear before
Code:
Enter value for X
Exactly, I'm not even sure how it skipped in the first place...it should prompt the user to enter 1 - 4, then afterwards, I have an array of "IF's", each leading to different functions. Entering anything but 1 - 4 will pause, then exit the program.


I can't even tell if I made a noob move, I see nothing wrong within the main() function...
[highlight=c]#include <iostream>
#include <conio.h>
using std::cout;
using std::endl;
using std::cin;
void Add(int x, int y);
void Subtract(int x, int y);
void Multiply(int x, int y);
void Divide(int x, int y);
void ExitProgram();
void main()
{
int userChoice;
int x=0,y=0;
cout << "===================" << endl;
cout << "= Calculator =" << endl;
cout << "===================" << endl;
cout << "1) Add" << endl;
cout << "2) Subtract" << endl;
cout << "3) Multiply" << endl;
cout << "4) Divide" << endl << endl;
cout << "Type the Number:" << endl;
cin >> userChoice;
if(userChoice < 1 || userChoice > 4)
{
cout << "Invalid choice. Press any key to exit." << endl;
_getch();
exit(0);
}
cout << "Enter Value for X: " << endl;
cin >> x;
cout << "Enter Value for Y:" << endl ;
cin >> y;
cout << endl;
switch(userChoice)
{
case 1:
Add(x,y);
break;
case 2:
Subtract(x,y);
break;
case 3:
Multiply(x,y);
break;
case 4:
Divide(x,y);
break;
default:
cout << "Invalid choice. Program will close now." << endl;
exit(0);
}
_getch();
}
void ExitProgram()
{
cout << endl << "Press any key to exit." << endl;
_getch();
exit(0);
}
void Add(int x,int y)
{
cout << "Result of addition of " << x << " and " << y << " is: " << x+y << endl;
ExitProgram();
}
void Subtract(int x,int y)
{
cout << "Result of subtraction of " << x << " and " << y << " is: " << x-y << endl;
ExitProgram();
}
void Multiply(int x,int y)
{
cout << "Result of multiplication of " << x << " and " << y << " is: " << x*y << endl;
ExitProgram();
}
void Divide(int x,int y)
{
cout << "Result of division of " << x << " and " << y << " is: " << x / y << endl;
ExitProgram();
}[/highlight]

Should Work ;]
Quote Originally Posted by Hassan View Post
[highlight=c]#include <iostream>
#include <conio.h>
using std::cout;
using std::endl;
using std::cin;
void Add(int x, int y);
void Subtract(int x, int y);
void Multiply(int x, int y);
void Divide(int x, int y);
void ExitProgram();
void main()
{
int userChoice;
int x=0,y=0;
cout << "===================" << endl;
cout << "= Calculator =" << endl;
cout << "===================" << endl;
cout << "1) Add" << endl;
cout << "2) Subtract" << endl;
cout << "3) Multiply" << endl;
cout << "4) Divide" << endl << endl;
cout << "Type the Number:" << endl;
cin >> userChoice;
if(userChoice < 1 || userChoice > 4)
{
cout << "Invalid choice. Press any key to exit." << endl;
_getch();
exit(0);
}
cout << "Enter Value for X: " << endl;
cin >> x;
cout << "Enter Value for Y:" << endl ;
cin >> y;
cout << endl;
switch(userChoice)
{
case 1:
Add(x,y);
break;
case 2:
Subtract(x,y);
break;
case 3:
Multiply(x,y);
break;
case 4:
Divide(x,y);
break;
default:
cout << "Invalid choice. Program will close now." << endl;
exit(0);
}
_getch();
}
void ExitProgram()
{
cout << endl << "Press any key to exit." << endl;
_getch();
exit(0);
}
void Add(int x,int y)
{
cout << "Result of addition of " << x << " and " << y << " is: " << x+y << endl;
ExitProgram();
}
void Subtract(int x,int y)
{
cout << "Result of subtraction of " << x << " and " << y << " is: " << x-y << endl;
ExitProgram();
}
void Multiply(int x,int y)
{
cout << "Result of multiplication of " << x << " and " << y << " is: " << x*y << endl;
ExitProgram();
}
void Divide(int x,int y)
{
cout << "Result of division of " << x << " and " << y << " is: " << x / y << endl;
ExitProgram();
}[/highlight]

Should Work ;]
Beat me too it.
Thank you guys. I'm still learning. :'(

Quote Originally Posted by Terell. View Post
Beat me too it.
He forgot #include "stdafx.h"

Thank you though.
Quote Originally Posted by BobFM View Post
Thank you guys. I'm still learning. :'(



He forgot #include "stdafx.h"

Thank you though.
#include "stdafx.h" wasn't needed here. The program compiles perfectly fine. Anyways, have fun !!
Quote Originally Posted by Hassan View Post


#include "stdafx.h" wasn't needed here. The program compiles perfectly fine. Anyways, have fun !!
Mine had a fatal error until I added it D: Probably different compiler or something...Dunno, but either way, thanks.
Yeah, I guess so. I am using Visual Studio 2010.
Anyways, Marked Solved.
Anytime, & if you want some credits at the end do something like

Code:
cout<<"yourcalculatorname,Developed  by BobFM\n";
Code:
cout << "Copyright© 2011 " << endl;
Or something in that sort.
Posts 1–14 of 14 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us