while do / while loop problems

need some help to fix this
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
# include <iostream>
# include <cstdlib>

using namespace std;

int main()
{


char next, name[40];
int  clientNumber, quantity, unitaryPrice;
float total, salesDay;
clientNumber=1;
salesDay=0;

   do
   {
    cout << "\n\n\tClient number: " << clientNumber; // would be better if it didn't show this
    cout << "\n\tClient name: "  ;
    cin.getline (name,40); // if i replace with cin >> name; works but only with one word.
    cout << "\n\tQuantity of articles : ";
    cin >> quantity;
    cout << "\n\tUnitary price: $";
    cin >> unitaryPrice;

    total= quantity*unitaryPrice;

    cout << "\n\tTotal: $" << total;



    salesDay+=total;
    clientNumber++;
    cout << "\n\tNext client (y/n): ";
    cin >> next;
    }

    while (next == 'y' || next == 'Y' );

    cout <<"\n\n\tTotal sales of the day: $"<<salesDay ;
    /* don't know how to make it display all the info of
    the clients and sales at the end after the costumer press 'n':

example

    Client name : Jon farmer
    Quantity of articles : 2
    Unitary price: $ 5
    Total: $10

    Client name ..... etc
     */
    cout <<"\n\n\n" ;


return 0;
}


i should make it ask the questions then ask if another costumer if the answer is yes repeat the questions, if not show the total of the sales and display the names , number of articles and total of each sell.

my problems
1)after the first costumer if i choose 'y' the program continues as it should but doesn't let me enter a new name goes right in to quantity of articles
(before i could make it ask for the name normal using
cin <<name;
but if the name had 2 or more words then it didn't work.

2) i'd rather if it didn't show at the beggining client number but i didn't know another way to keep the loop. (i'm just starting to learn)

cout << "\n\n\tClient number: " << clientNumber;

3) i don't know at the end how to make it display the list of costumers each one with it's respective quantity of articles ,unitary prices,total and the total sales of the day.

right now i can only make it display the total of the sales of the day


i would appreciate if someone could explain me or point me out how to solve my problems.

thanks in advance
Last edited on
1) You probably have junk or the last name entered left in the buffer, which you need to empty.
On line 38 or 39, right before do-while loops ends, add this line.
std::cin.ignore();

2) You could do something like this:
1
2
3
4
5
6
7
do
{
      if(clientNumber > 1)
           std::cout « "Client number: " « clientNumber;
    
      // continue code
      // ... 


3) After getting the name, perhaps you could put into an index in an array.
Then, output it at the end of the program.

Last edited on
thanks for the help

i put it on the line 36 and now is working.

i will start reading about arrays now :)
Topic archived. No new replies allowed.