Can anyone please help? Do-while statement

Hi everyone! I'm having a very hard time figuring out how to make my Do-While statement work in this Traveltour program. It's looping but it doesn't store the inputs i made at the first loop i made. It can only solve for the latest inputs given. Can anyone please help me out here :


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
82
83
84
85
86
87
88
89
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    string name;
    int age;
    char way_value,d_code,more;
    double fare,tax,total_amt;
  do
    {
    cout<<"Passenger Name: ";
    cin>>name;
    cout<<"Age: ";
    cin>>age;
    cout<<"====Way Value===="<<endl;
    cout<<"[1]One Way"<<endl;
    cout<<"[2]Two Way"<<endl;
    cin>>way_value;
    cout<<"====DESTINATION CODE===="<<endl;
    cout<<"[1]Manila-Cebu"<<endl;
    cout<<"[2]Manila-Davao"<<endl;
    cout<<"[3]Manila-Bacolod"<<endl;
    cout<<"[4]Manila-Tacloban"<<endl;
    cout<<"[5]Manila-Boracay"<<endl;
    cin>>d_code;
    cout<<" \n";
    switch(d_code)
    {
                  case'1':
                          fare=1000.00;
                          cout<<"MANILA-CEBU PURCHASED"<<endl;
                          break;
                  case'2':
                          fare=1500.00;
                          cout<<"MANILA-DAVAO PURCHASED"<<endl;
                          break;
                  case'3':
                          fare=699.00;
                          cout<<"MANILA-BACOLOD PURCHASED"<<endl;
                          break;
                  case'4':
                          fare=699.00;
                          cout<<"MANILA-TACLOBAN PURCHASED"<<endl;
                          break;
                  case'5':
                          fare=699.00;
                          cout<<"MANILA-BORACAY PURCHASED"<<endl;
                          break;
                  default:
                          cout<<"INVALID DESTINATION CODE [1/2/3/4/5]"<<endl;
                          break;
    }
    switch(way_value)
    {
                      case '1':
                              fare=fare*1;
                              cout<<"ONE WAY TICKET PURCHASED"<<endl;
                              break;
                      case '2':
                              fare=fare*2;
                              cout<<"TWO WAY TICKET PURCHASED"<<endl;
                              break;
                       default:
                               cout<<"INVALID INPUT! [1/2]"<<endl;
                               break;
    }
    cout<<"Would you like to buy more tickets?[Y/N]: "<<endl;
    cin>>more;
    if (age>=60)
            {
                tax=0.00;
                           }
                               else
                                     {
                                              tax=.10*fare;
                                                                  }
    total_amt=fare+tax;
} while(more=='Y'||more=='y');
    cout<<"Base Fare: "<<fare<<endl;
    cout<<"Government Tax: "<<tax<<endl;
    cout<<"TOTAL AMOUNT: "<<total_amt<<endl;
    
                                              
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
Please use code tags like 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  string name;
  int age;
  char way_value,d_code,more;
  double fare,tax,total_amt;
  do
  {
    cout<<"Passenger Name: ";
    cin>>name;
    cout<<"Age: ";
    cin>>age;
    cout<<"====Way Value===="<<endl;
    cout<<"[1]One Way"<<endl;
    cout<<"[2]Two Way"<<endl;
    cin>>way_value;
    cout<<"====DESTINATION CODE===="<<endl;
    cout<<"[1]Manila-Cebu"<<endl;
    cout<<"[2]Manila-Davao"<<endl;
    cout<<"[3]Manila-Bacolod"<<endl;
    cout<<"[4]Manila-Tacloban"<<endl;
    cout<<"[5]Manila-Boracay"<<endl;
    cin>>d_code;
    cout<<" \n";
    switch(d_code)
    {
      case'1':
      fare=1000.00;
      cout<<"MANILA-CEBU PURCHASED"<<endl;
      break;
      case'2':
      fare=1500.00;
      cout<<"MANILA-DAVAO PURCHASED"<<endl;
      break;
      case'3':
      fare=699.00;
      cout<<"MANILA-BACOLOD PURCHASED"<<endl;
      break;
      case'4':
      fare=699.00;
      cout<<"MANILA-TACLOBAN PURCHASED"<<endl;
      break;
      case'5':
      fare=699.00;
      cout<<"MANILA-BORACAY PURCHASED"<<endl;
      break;
      default:
      cout<<"INVALID DESTINATION CODE [1/2/3/4/5]"<<endl;
      break;
    }
    switch(way_value)
    {
      case '1':
      fare=fare*1;
      cout<<"ONE WAY TICKET PURCHASED"<<endl;
      break;
      case '2':
      fare=fare*2;
      cout<<"TWO WAY TICKET PURCHASED"<<endl;
      break;
      default:
      cout<<"INVALID INPUT! [1/2]"<<endl;
      break;
    }
    cout<<"Would you like to buy more tickets?[Y/N]: "<<endl;
    cin>>more;
    if (age>=60)
    {
      tax=0.00;
    } else {
      tax=.10*fare;
    }
    total_amt=fare+tax;
  } while(more=='Y'||more=='y');
  cout<<"Base Fare: "<<fare<<endl;
  cout<<"Government Tax: "<<tax<<endl;
  cout<<"TOTAL AMOUNT: "<<total_amt<<endl;
  
  
  system("PAUSE");
  return EXIT_SUCCESS;
}


It makes it so much easier to read and identify problems.
alright thanks for the advice. sorry im still a begginer in this forum =)
closed account (o3hC5Di1)
Hi there,

Not exactly sure what you mean, but you are changing these variables double fare,tax,total_amt; during the loop, so after the loop only the last values will remain in there.

If you want to store multiple entries in there you will have to use arrays - or one array of structs.
Then you can loop over the array(s) and get the data you need.

More info on arrays and structs:
http://cplusplus.com/doc/tutorial/arrays/
http://cplusplus.com/doc/tutorial/structures/

Hope that helps.

All the best,
NwN
Yes, I want to store multiple entries for the values of fare and tax. Then sum them all up for the total amount. Do you think i should use the arrays? Thanks for the Help!
If you want to keep the remaining name, age, etc perhaps you should test to see if they are input (loop has gone through at least once successfully) and not reassign them? A simple example of what I'm talking about:
1
2
3
4
5
6
7
8
9
10
  string name;
  ...
  do
  {
    if(name.empty()) // first run;
    {
      cout << "\nEnter your name:"; // remember this will only take one word.  If you want a full name with spaces between you'll need to use getline.  This introduces other problems when you use getline and cin >> though.
      cin >> name;
    }
  } while(...);
I was able to run this program and input name,age,etc. but when i Try to input ANOTHER data for the name,age, etc. it deletes the previous data i inserted. what i want to happen is to add the previous money values I inserted with the new value i inserted. Thanks for the help!
closed account (o3hC5Di1)
Yes, you will want to use arrays for that.
Ideally, actually, you'd want to use a vector of structs, example here:

http://stackoverflow.com/questions/8067338/c-vector-of-structs-initialization

Someone please correct me if there's a better way.
It's also possible to make an array of structs using pointers, but I gather that's kind of dirty work.

Let me know if you need any further help.

All the best,
NwN
Topic archived. No new replies allowed.