Program Help
Sep 20, 2016 at 11:39pm UTC
After i enter the budget given, it prints out the next question and just gives the percent calculation. I dont know why it skips over the budget_spent input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
int main() {
bool game = false ;
do {
char choice[25];
double budget_given, amount_spent, percent;
char dollar;
cout << " Would you like to use the budget calculator (y/n)?" << endl;
cin >> choice;
if (choice[0] == 'y' ) {
cout << "Enter the budget that you were allowed last year." << endl;
if (cin.peek() == '$' ) {
cin >> dollar >> budget_given;
}
else {
cin >> budget_given;
}
cout << " How much money did you spend last year?" << endl;
if (cin.peek() == '$' ) {
cin >> dollar >> amount_spent;
}
else {
cin >> amount_spent;
}
percent = (amount_spent / budget_given) * 100;
cout << " You used " << percent << " % " << "of the previous budget" << endl;
}
else if (choice[0] == 'n' ) {
cout << " Have a nice day" << endl;
game = false ;
}
} while (game != false );
system("pause" );
return 0;
}
Sep 20, 2016 at 11:41pm UTC
I think it has something do with the '$' entry. when i leave out the '$' then it works fine.
Sep 21, 2016 at 12:30am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;
int main()
{
bool game = false ;
do {
char choice[25];
double budget_given, amount_spent, percent;
char dollar;
cout << "Would you like to use the budget calculator (y/n)? : " ;
cin >> choice;
if (choice[0] == 'y' )
{
cout << "Enter the budget that you were allowed last year : " ;
if (cin.peek() == '$' )
{
cin >> dollar >> budget_given;
}
else
{
cin >> budget_given;
}
cin.ignore();
cout << "How much money did you spend last year? : " ;
if (cin.peek() == '$' )
{
cin >> dollar >> amount_spent;
}
else
{
cin >> amount_spent;
}
percent = (amount_spent / budget_given) * 100;
cout << "You used " << percent << " % " << "of the previous budget" << endl;
}
else if (choice[0] == 'n' ) {
cout << " Have a nice day" << endl;
game = false ;
}
} while (game != false );
system("pause" );
return 0;
}
Sep 21, 2016 at 12:38am UTC
I took the cin.ignore() and put it before the first cin.peek and i put one before the second cin.peek. it works but im not exactly sure how.
Topic archived. No new replies allowed.