-
Help with Code
#include <iostream.h> // cin and cout are defined in this header
void main()
{
double Input=0; // This will hold keyboard input
double Total=0; // This will hold the total
// Type a welcome message
cout<<"Welcome to the C++ Game Tutorials Adding Machine"
<<endl;
{
cout<<Enter a number please. or else
}
//Loop till the user enters -666
while(Input!=-666)
{
cout<<"Enter a value (-666 to exit): "; // Prompt user
cin>>Input; // Take input
Total = Total + Input; // Add total
cout<<endl // Print total
<<"Total = "<<Total<<endl;
}
cout<<"Good Bye!"<<endl<<endl;
}
1. Well I need a way to say that if 'Input' is not a number, it displays "This is not a number."
2. Well when I compile for some programs, the screen usually just flashes and exits. is there a way to kind of pause the program for the viewer to clearly see the contents of the program.
-
I can help with the second thing. However, for the second thing, you can use cin.get(), getch(), or system("pause")
Brendan
-
-
Check if number is Digit
Hi U,
take an input form user and check if it is digit. Some thing like this. Hope this helps.
Code:
#include <iostream.h> // cin and cout are defined in this header
#include<ctype.h>
void main()
{
double Input=0; // This will hold keyboard input
double Total=0; // This will hold the total
// Type a welcome message
cout<<"Welcome to the C++ Game Tutorials Adding Machine"
<<endl;
//Loop till the user enters -666
while(Input !=-666)
{
cout<<"Enter a value (-666 to exit): "; // Prompt user
cin>>Input; // Take input
if(isdigit(Input))//isxdigit() can check if it is hex and isdigit() will check for decimal and digit
{
Total = Total + Input; // Add total
cout<<endl<<"Total = "<<Total<<endl; // Print total
cout<<"Please Enter Something To Exit"<<endl;
cin.get()
}
else
{
cout<<"This is not a number." <<endl;
}
}
cout<<"Good Bye!"<<endl<<endl;
}
-