Archived

This topic is now archived and is closed to further replies.

masterconjurer

Something's wrong, can u please help me?

Recommended Posts

I''m learning C++. I''m using MS VC++ 6 Compiler. Anyway, i''m coding a small text-based calculator that does Addition, Subtraction, Multiplication and Division. I have the program finished except when I run it, I get a blank screen. If i press ENTER twice, my main menu appears but if you press another key, the program exits. How can I get it to appear without having to press ENTER twice and have the program not work? Below is the code for my program. Any help would be appreciated. Thanks. /////////////////////////// // Text-Based Calculator // /////////////////////////// #include <iostream.h> #include <conio.h> int main() { char action; cout << "Text-Based Calculator"; cout << "\n"; getch(); menu_loop: cout << "Main Menu"; cout << "\n"; cout << "<1> Addition\n"; cout << "<2> Subtraction\n"; cout << "<3> Multiplication\n"; cout << "<4> Division\n"; cout << "<5> Exit\n"; action = getch(); if (action == ''1'') { float add1, add2, sum; cout << "\n"; cout << "Addition Problem"; cout << "\n"; cout << "Input the first number: "; cin >> add1; cout << "\n"; cout << "Input the second number: "; cin >> add2; sum = add1 + add2; cout << "\n"; cout << "Your answer is: " << sum << ''\n''; goto menu_loop; } if (action == ''2'') { float sub1, sub2, diff; cout << "\n"; cout << "Subtraction Problem"; cout << "\n"; cout << "Input the first number: "; cin >> sub1; cout << "\n"; cout << "Input the second number: "; cin >> sub2; diff = sub1 - sub2; cout << "\n"; cout << "Your answer is: " << diff << ''\n''; goto menu_loop; } if (action ==''3'') { float mul1, mul2, prod; cout << "\n"; cout << "Multiplication Problem"; cout << "\n"; cout << "Input the first number: "; cin >> mul1; cout << "\n"; cout << "Input the second number: "; cin >> mul2; prod = mul1 * mul2; cout << "\n"; cout << "Your answer is: " << prod << ''\n''; goto menu_loop; } if (action == ''4'') { float div1, div2, quo; cout << "\n"; cout << "Division Problem"; cout << "\n"; cout << "Input the first number: "; cin >> div1; cout << "\n"; cout << "Input the second number: "; cin >> div2; quo = div1 / div2; cout << "\n"; cout << "Your answer is: " << quo << ''\n''; goto menu_loop; } else { // exit } return 0; }

Share this post


Link to post
Share on other sites
!!!! ARRRRRRRRGGGGGGGGH !!!!

!!!! GOTO USAGE !!!!

ok, shock over, goto is bad (unless you are doing asm or breaking out of a nested loop). It''s far simpler to use a while and switch statement combination here....


  

///////////////////////////

// Text-Based Calculator //

///////////////////////////


// dont use iostream.h, use iostream

#include <iostream>
#include <conio.h>

// instead of cout, now use std::cout, however if we are really

// lazy we can use the following line to stop us typing it in

// all the time

using namespace std;

int main()
{

char action;

cout << "Text-Based Calculator\n\n";

// don''t really think you need this line...

// getch();


bool done = false;

// force permanent loop

while(done == false)
{
// can output more than one thing at once,

// dont have to use seperate statements

cout << "Main Menu\n"
<< "<1> Addition\n"
<< "<2> Subtraction\n"
<< "<3> Multiplication\n"
<< "<4> Division\n"
<< "<5> Exit\n";
action = getch();

switch(action)
{
case ''1'':
{
float add1, add2, sum;
cout << "\n"
<< "Addition Problem"
<< "\nInput the first number: ";
cin >> add1;
cout << "\nInput the second number: ";
cin >> add2;

sum = add1 + add2;

cout << "\nYour answer is: "
<< sum << ''\n'';

}
break;
case ''2'':
{
float sub1, sub2, diff;
cout << "\nSubtraction Problem"
<< "\nInput the first number: ";
cin >> sub1;
cout << "\nInput the second number: ";
cin >> sub2;

diff = sub1 - sub2;

cout << "\nYour answer is: "
<< diff << ''\n'';

}
break;
case ''3'':
{
float mul1, mul2, prod;
cout << "\nMultiplication Problem"
<< "\nInput the first number: ";
cin >> mul1;
cout << "\nInput the second number: ";
cin >> mul2;

prod = mul1 * mul2;

cout << "\nYour answer is: " << prod << ''\n'';
}
break;
case ''4'':
{
float div1, div2, quo;
cout << "\nDivision Problem"
<< "\nInput the first number: ";
cin >> div1;
cout << "\nInput the second number: ";
cin >> div2;

quo = div1 / div2;

cout << "\nYour answer is: " << quo << ''\n'';
}
break;
default:
done = true;
break;
}
}

return 0;
}


The reason your original prog didn''t work was because you had loads of if statements rather than


  
if( a == 1)
{
}
else
if( a == 2)
{
}
else
{
// exit

}

Share this post


Link to post
Share on other sites