Would like some help with a do while loop.

I have this assignment that I am stumped on. I need to input a user id number such as 1111 and have the user then input the book code then the book cost. The problem I am having is I cannot figure out how to keep a running tally of one user ids inputs, add them up then pass them to the end of the program to output the grand totals of all students. I haven't been taught array's yet so the professor does not want us to use them. Any help would be great. Thanks

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
65
66
#include <iostream>
#include <iomanip>

using namespace std;
int main ()
{
//Declare Variables.         
int student_id;
char book_code;
float book_cost;
float tax_amount;
float book_subtotal;
const int SENTINEL = -9999;
const double TAX = .07;
float total_book_cost;
int number_books;
int total_books_sold;
double grand_total;

//Set Variables to Zero. 
number_books = 0;
total_book_cost = 0.00;
grand_total = 0.00;

//Set Decimal to two places.
cout << fixed << showpoint;
cout << setprecision(2);

//Input Data
cout<<"Please enter your Student ID, then press enter."<<endl;
cin>>student_id;
while (student_id != SENTINEL){
    cout<<"Please enter your Book Code, then press enter."<<endl;
    cin>>book_code;
    cout<<"Please enter the cost of the book, then press enter."<<endl;
    cout<<"$"; cin>>book_cost;
    tax_amount = book_cost * TAX;
    book_subtotal = book_cost + tax_amount;
    total_book_cost += book_subtotal; 
    number_books++;
    cout<<"\tStudent Textbook Purchases Report"<<endl;
    cout<<"********************************************"<<endl;
    cout<<"Student"<<"\tBook"<<"\tBook"<<"\tTax"<<"\tBook"<<endl;
    cout<<"Id"<<"\tCode"<<"\tCost"<<"\tAmount"<<"\tSubtotal"<<endl;
    cout<<"--------------------------------------------"<<endl;
    cout<<student_id<<setw(5)<<book_code<<setw(8)<<"$"<<book_cost<<
    setw(3)<<"$"<<tax_amount<<setw(4)<<"$"<<book_subtotal<<endl;
    cout<<endl;
    cout<<"Total number of books purchased:"<<setw(8)<<number_books<<endl;
    cout<<"Total books cost including tax:"<<setw(9)<<"$"<<total_book_cost<<endl;
    cout<<"Please enter your Student ID, then press enter."<<endl;
    cin>>student_id;
    }
grand_total += total_book_cost;
total_books_sold += number_books;
cout<<"**************************************************"<<endl;
cout<<"Grand Totals:"<<endl;
cout<<"Total number of students who purchased books:"<<endl;
cout<<"Total number of books sold:"<<endl;
cout<<"Total cost of all books and taxes:"<<setw(9)<<"$"<<grand_total<<endl;

//Can put grand totals here

          system("Pause");
          return 0;
}
Last edited on
Sorry for not taking a better look at this, but could you edit your post to include the code brackets? It makes it alot easier to read the code, plus it'll keep the white space so the indentations won't be removed which also helps a ton. And it adds line numbers! :) BTW, the code brackets can be inserted to your post with the button that has <> on it to the right.
Last edited on
I tried the code brackets and it won't keep my formatting. Any suggestions?
Press the Edit button to edit your post. Select the code with your mouse. Press the <> button on the right under format.
Thanks. I didn't highlight it before.
That is quite tricky to do without an array - but not impossible.

Some of your cout statements need to go before the loop, so they are not printed every time. Others need to go after the loop for the same reason.

Seen as you have 2 levels of data - the book, and the student, I think you need 2 nested loops. This will make it easier to keep track of the grand totals.

Hope all goes well.
If your professor doesn't want you to use arrays, I'm assuming he just wants an accumulated total of students not an array of the student id.


For every time the while loop is iterated correctly just have a count variable.
Studentcount++
Hope this helps!


edit: Just like you did with the books.
Last edited on
Here is a link with my code and the assignment: https://gist.github.com/4035166

I am so lost. Haven't had power for like a week and wasn't able to work on this. The professor wants us to have the user input all the data then output the totals. He has a specific way he wants it to output and it is driving me crazy. I cant seem to bunch the students inputs together without have a ton of variables.
Last edited on
No arrays, no functions??
ggrrr.. Sorry man I went to the gym?
I hope you're still there, I'm looking at your code atm
Hey. Yea i am still here. I have posted an updated code. I was able to do most of what i need but still not able to seperate the inputs. Also unsure how to get it to count the number of students.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>

using namespace std;
int main ()
{
          
    //Set Decimal to two places.
    cout << fixed << showpoint;
    cout << setprecision(2);

    int total_books = 0;
    int total_books_sold = 0;
    double grand_total = 0.0;

    const int SENTINEL = -9999;
    int     student_id = SENTINEL;
    //Input Data
    cout<<"Please enter your Student ID, then press enter."<<endl;
    cin>>student_id;

    while (student_id != SENTINEL){

        double  total_book_cost = 0.0;
        int     number_books = 0;
        char    book_code = '\0';
        while (true) 
        {
            cout<<"Please enter your Book Code, then press enter. Enter # to enter next student."<<endl;
            cin>>book_code;

            if (book_code == '#')
                break;

            float   book_cost;
            cout<<"Please enter the cost of the book, then press enter."<<endl;
            cout<<"$"; cin>>book_cost;

            const double TAX = .07;
            double tax_amount = book_cost * TAX;
            double book_subtotal = book_cost + tax_amount;

            total_book_cost += book_subtotal; 
            number_books++;

            cout<<"\tStudent Textbook Purchases Report"<<endl;
            cout<<"********************************************"<<endl;
            cout<<"Student"<<"\tBook"<<"\tBook"<<"\tTax"<<"\tBook"<<endl;
            cout<<"Id"<<"\tCode"<<"\tCost"<<"\tAmount"<<"\tSubtotal"<<endl;
            cout<<"--------------------------------------------"<<endl;
            cout<<student_id<<setw(5)<<book_code<<setw(8)<<"$"<<book_cost<<
                setw(3)<<"$"<<tax_amount<<setw(4)<<"$"<<book_subtotal<<endl;
            cout<<endl;

        };


        grand_total += total_book_cost;
        total_books_sold += number_books;
        
        
        cout<<"Total number of books purchased:"<<setw(8)<<number_books<<endl;
        cout<<"Total books cost including tax:"<<setw(9)<<"$"<<total_book_cost<<endl;
        cout<<"Please enter your Student ID, then press enter."<<endl;
        cin>>student_id;
    }
    
    cout<<endl;
    cout<<"**************************************************"<<endl;
    cout<<"Grand Totals:"<<endl;
    cout<<"Total number of students who purchased books:"<<endl;
    cout<<"Total number of books sold:"<<setw(21)<<total_books_sold<<endl;
    cout<<"Total cost of all books and taxes:"<<setw(15)<<"$"<<grand_total<<endl;
    cout<<"**************************************************"<<endl;
    cout<<endl;

    //Can put grand totals here

    system("Pause");
    return 0;
}
Yeah, I got it to that point aswell haha, but wanting to put all the inputs in an array. The way he wants it to output seems ridiculous if he doesn't want arrays.

edit: You sure he doesn't want any array? One of his variable says
"Book codes"
Last edited on
if (book_code == '#')

Why not make this the condition for the inner while loop instead of an infinite loop?

I am not a big fan of multiple statements on one line.

Also with variable names I would do this sort of thing:

1
2
3
int     NumBooks = 0;  //Number of Books
double  TotBookCost = 0.0; //Total Book Cost


So you can abbreviate a bit, don't use underscores - they make the name too long. Keep the comments at the declaration.

Now with the loops.

The inner loop is for the books, the outer is for the student. Can you figure out what goes before, at the beginning, and after the loops? Also where to put the student count variable.

HTH
Topic archived. No new replies allowed.