if else loop not inputting good data and bad data right

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
so the goal is to get my program to say bad input and ask for the input again if the characters input are not s, c, g, S, C, G, however ive tried every configuration i know and i can either get it to accept nothing, accept everything, or do nothing the first 2 times you enter something and then accept anything... please help me with anything you can i would be so grateful, this was just the last thing i tried


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


using namespace std;

int main()
{ 

  char ride; 
  char s, S;
  char c, C;             
  char g, G;  
  
  int startHour;
  int startMinute;
  int endHour; 
  int endMinute;
  
  int rideTime;
  rideTime = endHour - startHour;
  
  
  cout << "Name: Brandon Holtz\n";
  cout << "Week 4 Program B\n";
  cout << "CRN 26670\n";
                                   
  cout << "This program will calculate the amount due "
       << "\n after a rickshaw ride.\n"
       << " \nto calculate fare for a single ride, input S (or s) " 
       << " \nto calculate fare for a couples ride, input C (or c )"
       << " \nto calculate fare for a group ride, input G ( or g)"
       << " \n\nInput S, C, or G \n";
  cin >> ride ;

     if (ride ==s||c||g||S||C||G)
      {
      ride =1;  
     cin >> ride;
     }
     if (ride!= s||c||g||S||C||G)
     ride = 0;
        cout << "bad input ";
       cout << "\ninput an int " ;
        cin.clear();
       cin.ignore (10,'\n');
       cin >> ride;
       

   cout << fixed << showpoint;
   cout << setprecision (2);

  
  if (ride == 's' || ride== 'S')
  {   
     cout << "\nEnter time the ride started seperating hour and minutes by a space.\n";
     cin >> startHour >> startMinute;
     cout << "\nEnter time the ride ended seperating hour and minutes by a space. \n";
     cin >> endHour >> endMinute;
     cout << endHour - startHour << "h " << endMinute - startMinute << "m\n";
   
    if (endHour - startHour == 0||1||2||3||4||5||6||7||8||9 && endMinute - startMinute > 30) 
     {
     cout << "\nThe ride will be $" << 7 + (1.50 * ((endMinute - startMinute)- 30));
     }
  
  if  (endHour - startHour == 0 && endMinute - startMinute <= 30)
     {
     cout << "\nThe ride will be $7.00 please\n";
}

       system("pause");
  return EXIT_SUCCESS; 
     
}
  if (ride== 'c' || ride== 'C');
   { 
     cout << "\nEnter time the ride started seperating hour and minutes by a space.\n";
     cin >> startHour >> startMinute;
     cout << "\nEnter time the ride ended seperating hour and minutes by a space. \n";
     cin >> endHour >> endMinute;
     cout << endHour - startHour << "h " << endMinute - startMinute << "m\n";
  {
  if (endHour - startHour == 0 && endMinute - startMinute <= 20)
     cout << "The ride will be $10.50 please";
}
}
  if (ride == 'g' || ride== 'G')
  {
     cout << "\nEnter time the ride started seperating hour and minutes by a space.\n";
     cin >> startHour >> startMinute;
     cout << "\nEnter time the ride ended seperating hour and minutes by a space. \n";
     cin >> endHour >> endMinute;
     cout << endHour - startHour << "h " << endMinute - startMinute << "m\n";
   {   
   if (endHour - startHour == 0 && endMinute - startMinute <= 15)
     cout << "\nThe ride will be $16.00 please\n" ;
     
          }
     }


    
     
     
  

  
  system("pause");
  return EXIT_SUCCESS
Last edited on
Can you please re-post this using tags (or edit this one).
what do you mean with tags?
what do you mean with tags?


Use the code tags, they look like <> in the editor menu.

It will make your code look like this!

Best,

HMW
any help is greatly appreciated guys!
Last edited on
anyone have any ideas what i could do to make the program not accept bad input and loop around to ask again, and also accept good data... it just keeps acting funky and ive tried rewriting it mulitiple times with no luck
Lines 40 and 45 are wrong. You can't do a == b||c, you have to do a == b || a == c.
Line 113: system("pause");

Compiles, but use cin.ignore(); instead.
now it says bad input no matter what you put first, then accepts whatever input you put whether good or bad, i also tried changing my if statement to a while, same result or it wouldnt compile at all

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <cmath>  
#include <iostream>
#include <iomanip>


using namespace std;

int main()
{ 

  char ride; 
  char s = 1;
  char c = 1;             
  char g = 1;
  char S = 1;
  char C = 1;
  char G = 1;  
  
  int startHour=0;
  int startMinute=0;
  int endHour=0; 
  int endMinute=0;
  
  int rideTime=0;
  rideTime = endHour - startHour;
  
  
  cout << "Name: Brandon Holtz\n";
  cout << "Week 4 Program B\n";
  cout << "CRN 26670\n";

                                   
  cout << "This program will calculate the amount due "
       << "\n after a rickshaw ride.\n"
       << " \nto calculate fare for a single ride, input S (or s) " 
       << " \nto calculate fare for a couples ride, input C (or c )"
       << " \nto calculate fare for a group ride, input G ( or g)"
       << " \n\nInput S, C, or G \n";
  cin >> ride;
    
    if  (ride == s||ride == c || ride == g || ride ==S || ride ==C || ride == G)
      {
             cin >> ride;
}
   else if   (ride != s || ride != c || ride != g || ride != S || ride != C || ride != G)
{
    cout << "bad input ";
    cout << "\ninput a new character\n " ;
    cin >> ride;
}

       

   cout << fixed << showpoint;
   cout << setprecision (2);

  
  if (ride == 's' || ride== 'S')
  {   
     cout << "\nEnter time the ride started seperating hour and minutes by a space.\n";
     cin >> startHour >> startMinute;
     cout << "\nEnter time the ride ended seperating hour and minutes by a space. \n";
     cin >> endHour >> endMinute;
     cout << endHour - startHour << "h " << endMinute - startMinute << "m\n";
   
    if (endHour - startHour == 0||1||2||3||4||5||6||7||8||9 && endMinute - startMinute > 30) 
     {
     cout << "\nThe ride will be $" << 7 + (1.50 * ((endMinute - startMinute)- 30));
     }
  
  if  (endHour - startHour == 0 && endMinute - startMinute <= 30)
     {
     cout << "\nThe ride will be $7.00 please\n";
}

       system("pause");
  return EXIT_SUCCESS; 
     
}
  if (ride== 'c' || ride== 'C');
   { 
     cout << "\nEnter time the ride started seperating hour and minutes by a space.\n";
     cin >> startHour >> startMinute;
     cout << "\nEnter time the ride ended seperating hour and minutes by a space. \n";
     cin >> endHour >> endMinute;
     cout << endHour - startHour << "h " << endMinute - startMinute << "m\n";
  {
  if (endHour - startHour == 0 && endMinute - startMinute <= 20)
     cout << "The ride will be $10.50 please";
}
}
  if (ride == 'g' || ride== 'G')
  {
     cout << "\nEnter time the ride started seperating hour and minutes by a space.\n";
     cin >> startHour >> startMinute;
     cout << "\nEnter time the ride ended seperating hour and minutes by a space. \n";
     cin >> endHour >> endMinute;
     cout << endHour - startHour << "h " << endMinute - startMinute << "m\n";
   {   
   if (endHour - startHour == 0 && endMinute - startMinute <= 15)
     cout << "\nThe ride will be $16.00 please\n" ;
     
          }
     }


    
     
     
  

  
  system("pause");
  return EXIT_SUCCESS; 
}


 
Last edited on
This is long overdue perhaps, but if you look at line 45:


else if (ride != s || ride != c || ride != g || ride != S || ride != C || ride != G)
You do not have the chars within ''. You do, however on line 58:


if (ride == 's' || ride== 'S')
Shouldn't the chars always be like 's', 'c', 'g', etcetera?

Someone with more knowledge than me (I am a beginner!) might shed some more light on this.

Best,

HMW
Line 41 & 45 are incorrect. To tell the compiler you want the if statement to read a function you have to put the letters (s,c,g, etc.) in single quotations.

e.g. if ( cost = 'S' ) { .. }

Another tip, if you want to clean up your program you can substitute those if statements with switch. Search for switch in the link below.

http://cplusplus.com/doc/tutorial/control/


Last edited on
Topic archived. No new replies allowed.