Easy answer needed!! Will not allow input

I am very new at C++, but I have perused the forum and saw only one similar quesiton. I am writing a program that will prompt the user for inputs, one a name, the other is a whole dollar amount. The prog will determine and display the amount of quarters, dimes, nickels, and pennies in that amount. However it will allow the first input (cin:name;), then goes past the cin:num; (input of dollar amount) and does the computations with a self-generated number (3647105) and displays them acordingly.

Here is my code:

#include <iostream> //allows program to use input and output
using std::cout; //program uses cout
using std::cin; //program uses cin
using std::endl; //program uses endl

int main ()
{
int name; //name of person using program
int num;//number of conversions
int dollar; //creates integer for dollar inputs
int halfdollar; //creates integer for half dollar amounts
int quarter; //creates integer for quarter amounts
int dime; //creates integer for dime amounts
int nickel; //creates integer for nickel amounts
int penny; //creates integer for penny amounts



std::cout<<"Enter your first name please. "; //Prompts for Users name
std::cin>>name; //Input of User name
std::cout<<"Welcome "<<name<<::std::endl;

std::cout<<"This program will convert a positive whole dollar amount to coin amounts.\n"; //Declartive statement of program

std::cout<<"Enter Dollar amount here:"; //Prompts for dollar amount
std::cin>>num; //input for dollar amount

halfdollar=num/2;
quarter=num/4;
dime=num/10;
nickel=dime*2;
penny=nickel*5;


std::cout<<name<<", the coin amounts are as followed:\n";


std::cout << "Half dollar:\t\t" <<halfdollar<< endl;
std::cout << "Quarter:\t\t" <<quarter<< endl;
std::cout << "Dime:\t\t" <<dime<< endl;
std::cout << "Nickel:\t\t" <<nickel<< endl;
std::cout << "Penny:\t\t" <<penny<< endl;

Results:
Enter your first name please. joh
Welcome 1242232
This program will convert a positive whole dollar amount to coin amounts.
Enter Dollar amount here:1242232, the coin amounts are as followed:
Half dollar: 694732
Quarter: 347366
Dime: 138946
Nickel: 277892
Penny: 1389460
Press any key to continue . . .


}
You don't need the std:: prefix for cout, cin, and endl, after all you are using them. That's a detail, though.
http://cplusplus.com/doc/tutorial/namespaces/

cin leaves the newline in the stream (in other words, your Enter is still in the stream. Yes, that's also there). The easy way to get rid of it is cin.ignore() (which eats one character). The harder way but the way that clears everything in the stream (including the newline) is:
cin.ignore(std::numeric_limits<streamsize>::max(),'n');

You'll need to #include <limits>.

-Albatross
Last edited on
It is because the name variable is an int which confuses every thing.
He should change name variable to std::string
Last edited on
*scrolls up*
Whoop- right. That's another detail, and using an std::string should fix majority of the problem (although the newline will still be left in the steam).

@OP:
To use strings, you will need to
#include <string>

-Albatross
Last edited on
Everyone, thanks I ended up commenting out "//" anything with the name input or reference. It ran correctly. This is for a five week class that ends on 6/7/10, and I have been busy with our three week old son. I really want to work on building my own code, and this did not need the name, I just wanted more than the expected. I need to just add a repitition (if..else) and that will do it, then after it is good enough class, I will modify it later to tweak it accordingly.

I have been using cplusplus.com quite a bit, it is nice. We have not touched on namespace in our class. I was initially looking at using a few cin's (name, reiterations,dollar amount) and building arrays around it. Using the reiterations as the terminal number and also as array designation. however it crashed my compiler. I will work on that later. thank you both again.
Topic archived. No new replies allowed.