program won't allow new user input entries after 1st run loops back

Please input the code i'm providing below into your compiler so you can see the issue i'm speaking of, as it's sort of hard to just explain without seeing it. For some reason my program works fine the first time it runs thru, but when it loops back around after the 1st time, the program displays prompt for user to enter new information, however it doesn't actually stop and let the user input anything the way it did the 1st time, instead just sets all the user entries to null, or empty chars and strings. I've tried many suggestions from ppl, such as using cin.get() instead of cin.getline(), and i've changed the loop structure of the program so many times I'm getting dizzy, and STILL haven't been able to get it to work. Please Please someone come to my rescue and help me solve this problem.

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <cstdlib>
#include <iomanip>
#include <iostream>


using namespace std;


char Func_menu();               //Menu Function prototype //
void swapChars(char*, char, char);  //character swap function prototype
int countChar(char*, char);         //character counter function prototype
void get_sentence(char*);               //function to aquire string from user



void get_sentence(char* userline)                //get_sentence function header
{    
      cout << "\n\nWelcome To Ms. String's String Editor!"
             "\n=============================="
              "\nPlease Enter A String:\n";     //get string from user
               cin.get(userline, 80);
               }

char Func_menu()    //Definition header
{   
    
    char valid_entry;                  //for loop to ensure valid menu selection
    char selection;                      //i/o menu choices
      
      /*
      This function allows user to select from initial menu options, 
      and then passes their choice back to the main() function
      which then calls the appropriate string modifier function based
      on users choice.
      */
      do
      {
      selection = 'x';
      
      if(selection == 'x')
      {
      cout << "\nString Editor"
              "\n---------------------------"
              "\nPlease Select from the Options Below:\n"
              "\n---------------------------"
              "\n(A)Substitute Character"
              "\n(B)Count Character"
              "\n(E)Exit Program"
              "\nPlease Make a Selection Using Letters in Parenthesis:\n\n";
      cin >> selection;      
      }
            
                switch(selection)
                     {
                     case 'a':
                     case 'A':
                         valid_entry == 'y';
                         return selection;
                          break;         
                
                   case 'b':
                   case 'B':
                        valid_entry == 'y';
                        return selection;
                        break;
                   
                   case 'e':
                   case 'E':
                         valid_entry == 'y';
                         return selection;
                         break;
                   default:
                           valid_entry = 'n';
                           
                           cout << "\nEntry Not Recognized, Please Retry\n";
                         system("PAUSE");
                           break;
                   }
            }while(valid_entry == 'n');
}

void swapChars(char* str, char c1, char c2) 
 /*
      The swapChars() function scans the user inputted
      string for oldchar, and then swaps it with 
      user inputted newchar. 
      
      */

{
  for (int i=0;i<80;i++) if (str[i]==c1) str[i]=c2;
}



int countChar(char* str, char c) 

 /*
 The countChar() function counts how many times a
 particular char appears in the string, then replaces
 each occurance of that char with the number order in which it appeared.   
      
 */
{
  int count=0;
  for (int i=0;i<80;i++)
  if (str[i]==c) 
  { 
    count++;
    if (count<=9) 
    {str[i]=count+48;
    }
  }
  return count;
}


int main()
{
    char newstring = ' ';                 //for user to choose to change the entered string
    char go_again = 'y';                  //char for invalid entry loop check
    char countedchar;                    //the character to be counted
    int cuanto;                          //how many times the character was counted
    char oldchar;                        //character to be replaced
    char newchar;                        //replacement character
    char sentence[80];                   //string array
    char menu_choice = ' ';              //entry to continue or not
    
    
    
    do{
    
    get_sentence(sentence);
               
               cout << "\nYou Entered: " << sentence <<"\n"; //show user entry
               
     menu_choice = Func_menu();        //calls menu function
                 
                 switch(menu_choice)
                     {
                     default:
                                system("PAUSE");
                                cout << "\nInvalid Entry, Please Retry.\n";
                                break;
                     case 'A':
                     case 'a':
                          
                          cout << "\nSubstitute Character\n"
                          "==========================\n"
                          "Enter Character To Be Scanned:\n";
                          
                          cin >> oldchar;
                          
                          cout << "\nEnter Character To Be Substituted:\n";
                          
                          cin >> newchar;
                          
                          swapChars(sentence, oldchar, newchar);
                          
                          cout <<"\nThe Updated String Is:\n"
                               << sentence << "\n\n"; 
                               
                          
                          system("PAUSE");
                        break;
                        
                     case 'b':
                     case 'B':
                          
                          cout << "\nCharacter Count\n"
                        "======================\n"
                        "Enter Character To Be Counted:\n";
                        
                        cin >> countedchar;
                        
                        cuanto = countChar(sentence, countedchar);
                        
                        cout << "\nThe Revised String Is:\n"
                        << sentence <<"\n"
                        "The Character " << countedchar <<
                        " appeared " << cuanto << " Times.\n\n";
                        
                          system("PAUSE");
                
                        break;
                        
                        case 'e':
                        case 'E':
                                 cout << "SESSION TERMINATED, "; 
                                 system("PAUSE");   //DISPLAY END MESSAGE
                                   return 0;
              
                        break;
                        } //end switch statement
              }while( (menu_choice != 'e') || (menu_choice != 'E') );
    
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
I found a couple of things to change to get it working (at least, by my very limited checks, both in Func_menu().
You have
valid_entry == 'y'
which returns false (as valid_entry does not equal 'y')
when you mean
valid_entry = 'y'
valid_entry is assigned the value 'y'

Also, just before
cin >> selection;
you need
cin.clear();

Why it appears you sometimes need to do this and other times not I have yet to work out (I've been programming for may years, but only recently started working with C++), perhaps one of the experts who read herre cna help on that one?


closed account (z05DSL3A)
First, lines 57,63, and 69 have '==' I think you want '='

Second, It is not a good idea to call system("pause") in your code.

Finally, after each 'cin >> ...' call put a cin.ignore(); you are likely having some junk left in the stream. I only suggest this as a test not as a proper method of handling stream input. It would be worth spending a little more time reading about the various stream functions to see what is left in the stream when it has finished.
Last edited on
thanks for all the help, much appreciated, cleared alot of my confusion.
Topic archived. No new replies allowed.