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 ?
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)
Here is a SS of what happens when I first build / run: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's what SHOULD happen:
Sorry if I wasn't perfectly clear, feel free to ask questions.Code:================ = Calculator = ================ 1) Add 2) Subtract 3) Multiply 4) Divide (Type the number) "User types number here" ( Goes to _add(), _subtract(), _multiply(), or _divide() )
Edit: If you can't see the screen shot, download the attachment.
Last edited by BobFM; 08-20-2011 at 09:49 AM. Reason: Screen Shot didn't show up
= Incomplete
= Learning/trying
= Completed
= Owned and stomped!
Languages:
DOS/Batch
HTML/JavaScript
C++ - (But dedicated. ;D)
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 ?
Last edited by Terell.; 08-20-2011 at 09:42 AM.
Warrock Minion 8-13-2011 - N/AA.V.A Minion since 11-1-11 - 11-12-11
Sure that'll work.
Warrock Minion 8-13-2011 - N/AA.V.A Minion since 11-1-11 - 11-12-11
Photo here
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
Last edited by BobFM; 08-20-2011 at 09:47 AM.
= Incomplete
= Learning/trying
= Completed
= Owned and stomped!
Languages:
DOS/Batch
HTML/JavaScript
C++ - (But dedicated. ;D)
So you want
to appear beforeCode:cout << "===================\n"; cout << "1) Add\n"; cout << "2) Subtract\n"; cout << "3) Multiply\n"; cout << "4) Divide\n"; cout << "(Type the number)\n";
Code:Enter value for X
Warrock Minion 8-13-2011 - N/AA.V.A Minion since 11-1-11 - 11-12-11
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...
Last edited by BobFM; 08-20-2011 at 09:57 AM.
= Incomplete
= Learning/trying
= Completed
= Owned and stomped!
Languages:
DOS/Batch
HTML/JavaScript
C++ - (But dedicated. ;D)
[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 ;]
BobFM (08-20-2011)
Anytime, & if you want some credits at the end do something like
Code:cout<<"yourcalculatorname,Developed by BobFM\n";Or something in that sort.Code:cout << "Copyright© 2011 " << endl;
Last edited by Terell.; 08-20-2011 at 10:24 AM.
Warrock Minion 8-13-2011 - N/AA.V.A Minion since 11-1-11 - 11-12-11
BobFM (08-20-2011)
Last edited by BobFM; 08-20-2011 at 10:34 AM.
= Incomplete
= Learning/trying
= Completed
= Owned and stomped!
Languages:
DOS/Batch
HTML/JavaScript
C++ - (But dedicated. ;D)
Yeah, I guess so. I am using Visual Studio 2010.
Anyways, Marked Solved.
BobFM (08-20-2011)