newbie C++ question

UnfaTeFuL

Limp Gawd
Joined
Jun 23, 2002
Messages
460
So I just started to take up C++ again and doing some refresher coding and I'm having some trouble here. I wrote up a random program on visual C++ express and compiled it from this:

Code:
#include <iostream>
#include <ostream> 

using namespace std;



int main()
{
	int number;
	int result;
	int choice;

	do{
		cout << "\n";
		cout << "0 - Quit the program. \n";
		cout << "1 - Multiplier tool. \n";
		cout << "Enter choice here: ";
		cin >> choice;

	if (choice == 1){
	cout << endl;
	cout << "Enter a number to be multiplied by 3: ";
	cin >> number;
	cin.ignore();
	result = number*3; //action
	cout << "The result is " << result << " Thank you! Press any key to exit the program.\n";	
	}//end of if code
	
	if (choice != 1 || 0){
	cout << endl;
	cout << "Error! Please enter a 1 or 0. \n";
	}

	}while(choice!=0);//end of do code
	
	
	cout << "Press any key to quit this program.";
	cin.get();

	return 0;

}

The problem is that I can't get the "Press any key to quit this program." to show up. The program would instantly exit when I enter 0. Everything else seems to work fine.
 
Out of curiosity, what happens if you comment out that cin.ignore()?
 
Wingy said:
Out of curiosity, what happens if you comment out that cin.ignore()?

Edit: I can't see your problem. Compiling your code and running it works fine for me.
 
instead of pausing with cin.get you could try doing this:

while(!kbhit());

kbhit() is defined in conio.h i believe...
 
Cyrilix said:
Edit: I can't see your problem. Compiling your code and running it works fine for me.

Are you sure about that? If you hit 0, it throws the error message (which is not what it's supposed to do).

CodeX said:
instead of pausing with cin.get you could try doing this:

while(!kbhit());

kbhit() is defined in conio.h i believe...

Would depend on what compiler he's using. conio.h isn't a standard header in C/C++, and was originally a Borland extension to the language. GCC has flaky support for it (you can link to the shared conio library and see if it works), and I'm not sure about VC/C++ as I have not tried.
 
I didnt check, but I'm assuming you are ignoring one character of windows \r\n newlines, so when you read again you are getting the \n. do cin.ignore('\n'); instead. And yes thats a logic error on the if statement, you are doing if (some or false).
 
Your indentation is not proper. You should really try to keep things at the correct indention level since it makes your code much easier to read.
 
Didn't even notice that logic error the first time I glanced at it.

instead of if(choice != 1 || 0) use if(choice != 1 && choice != 0) or if(!(choice == 1 || choice == 0))
 
CodeX said:
Didn't even notice that logic error the first time I glanced at it.

instead of if(choice != 1 || 0) use if(choice != 1 && choice != 0) or if(!(choice == 1 || choice == 0))


I see where the problem was with pressing 0. if(!(choice == 1 || choice == 0)) work correctly.

nstead of pausing with cin.get you could try doing this:

while(!kbhit());

kbhit() is defined in conio.h i believe...

That hit the spot. I took out cin.get() and replaced it with while(!kbhit()); and it didn't close the program instantly until I saw my message and hit any key. For some reason cin.get() worked in a some simpler programs and didn't for this one. Thanks for all the help guys!
 
For the pause at the end of the program, you can also do it like the following if you don't want to do the conio.h way.

Code:
#include <cstdio>
using namespace std;

void myPause() {
    fflush(stdin);
    printf("Press ENTER to exit . . . ");
    fflush(stdout);
    getchar();
}

int main() {
    myPause();
}
 
Back
Top