Help

So as per usual I am new to this stuff.

I am trying to figure out how to make my program read back the name of a user input that will include spaces.

For some reason the get line function is not working.

Help?



#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;


void showChoices ();
void grandtotal(double & total);

int main()
{
// variables
string item;
double price;
double total;
char menu;
cout << fixed << setprecision(2) << endl;

do
{
showChoices();
cin >> menu;
cout<< endl;

switch (menu)
{
case 'a':

cout << "Enter item name: " << endl;
getline(cin, item);
cout << endl;


cout << "Enter price of " << item << ": " << endl;
cin >> price;
cout << endl;


cout << item << " is " << price << endl;
cout << endl;


break;



case 't':
cout << "Subtotal: " << endl;
cout << total;
cout << endl;

break;


case 'q':
cout << "Grand Total: " << total << endl;
cout << "enter 'q' to quit: " << endl;
cout << endl;
break;

default:
cout << "Invalid input." << endl;

}
}
while (menu != 'q');
return 0;
}

void showChoices()
{
cout << "Select an option" << endl;
cout << endl;
cout << "a. Add an item to the cart:" << endl;
cout << "t. Total: " << endl;
cout << "q. Quit" << endl;
cout << endl;
}
My next question is how would you use the same infinite loop to do a running total?

would I use a void function?
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
61
62
63
64
#include <iostream>
#include <cctype>
#include <string>
#include <iomanip>

char get_choice()
{
    std::cout << "\nSelect an option\n\n"
                 "a. Add an item to the cart:\n"
                 "t. Total:\n"
                 "q. Quit\n\n" ;

    char choice ;
    std::cin >> choice ;
    return std::tolower(choice) ; // http://www.cplusplus.com/reference/cctype/tolower/
}

void get_item( std::string& name, double& price )
{
    // there is a new line left in the input buffer after the formatted input in
    // get_choice(). we need to throw that away, before the next unformatted input
    // see: http://www.cplusplus.com/forum/general/69685/#msg372532
    std::cin.ignore( 1000, '\n' ) ; // http://www.cplusplus.com/reference/istream/istream/ignore/

    std::cout << "enter item name: " ;
    std::getline( std::cin, name ) ;

    std::cout << "enter price of '" << name << "': " ;
    std::cin >> price ;
    // TODO: the user may not enter a valid number.
    //       for instance, the user may enter 'abcd'
    //       we may need to add a fix for that.
}

int main()
{
    std::cout << std::fixed << std::setprecision(2) ;
    double total = 0 ;

    char choice ;
    while( ( choice = get_choice() ) != 'q' )
    {
        std::string name ;
        double price = 0 ;

        switch(choice)
        {
            case 'a' :
                get_item( name, price ) ;
                std::cout << "item: '" << name << "'  price: " << price << '\n' ;
                total += price ;
                break ;

            case 't' :
                std::cout << "subtotal: " << total << '\n' ;
                break ;

            default:
                std::cout << "invalid input.\n" ;
        }
    }

    std::cout << "grand total: " << total << '\n' ;
}
Topic archived. No new replies allowed.