I'm Stumped: program not looping back to beginning correctly

I'm trying to finish a program that prompts user to input a string, then a menu is displayed giving them options to swap characters or count a character, and then display the newly revised string back to them. At the end of all this it should prompt user if they wish to continue (go back to beginning) or end program. I tried using a do/while loop for the continue option but when it runs a 2nd time the prompt for user to enter a new string doesn't stop to allow the user to enter anything, it just inserts an empty array string instead.

The problem is in the main() function. Also having problems getting it to loop back on invalid entries, I think the problem could be in the default case of the switch loop but i'm not too sure.

Here is my code: it should compile and run so you can see what i'm talking about. thanks for any help i can get this has really had me stumped and it was due thursday.

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
#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);



char Func_menu()    //Definition header
{   
    char selection;                      //i/o menu choices
      
      
      cout << "\nString Editor"
              "\n---------------------------"
              "\nPlease Select from the Options Below:\n"
              "\n---------------------------"
              "\n(A)Substitute Character"
              "\n(B)Count Character"
              "\nPlease Make a Selection Using Letters in Parenthesis:\n\n";
      cin >> selection;      
      
            
                switch(selection)
                     {
                     case 'a':
                     case 'A':
                          return selection;
                          break;         
                
                   case 'b':
                   case 'B':
                        
                        return selection;
                        break;
                   
                   default:
                           return selection;
                           break;
                   }
}

void swapChars(char* str, char c1, char c2) 
{
  for (int i=0;i<80;i++) if (str[i]==c1) str[i]=c2;
}



int countChar(char* str, char c) 
{
  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 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
    
    
    go_again = 'y';
    
    do
    {
    cout << "\n\nWelcome To Ms. String's String Editor!"
             "\n=============================="
              "\nPlease Enter A String:\n";     //get string from user
       
        cin.getline(sentence, 80);
        
    cout << "\nYou Entered: " <<sentence <<"\n"; //show user entry

     menu_choice = Func_menu();        //calls menu function
                
                 switch(menu_choice)
                     {
                     default:
                                cout << "\nInvalid Entry, moron.\n";
                                go_again = 'y';
                                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"; 
                               
                          cout << "\nDo You Want to Go Again?(y/n):\n";
                        
                        cin >> go_again;
                        
                        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.";
                        
                        cout << "\nDo You Want to Go Again?(y/n):\n";
                
                        cin >> go_again;
              
                        break;
                        
                        
                        
                        } 
                }while(go_again == 'y');
    
   
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
can anyone help me out as to why the cin.getline statement seems to get skipped everytime the program loops back around?? It allows user to input a string the first time, but then anytime after it skips the user input and sets it as an empty string? please help very confused by this. thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
do
    
{
    cout << "\n\nWelcome To Ms. String's String Editor!"
             "\n=============================="
              "\nPlease Enter A String:\n";     //get string from user
       
        cin.getline(sentence, 80);
        
    cout << "\nYou Entered: " <<sentence <<"\n"; //show user entry

     menu_choice = Func_menu();        //calls menu function 
what about trying this:

getline(cin, sentence, 80);
when you go thought to the getline(sentence, 80)
the user inputs a string and when the loop goes around again the same string should be in there so you have to either make a for loop and declare a different string every time or erase the string from the input


*this might be wrong

i tried it out and made it so the getline(sentence, 80)
was changed to
cin >> sentence;
and it worked fine


Last edited on
ecker676 - - your's didn't work because it reads getline(cin, sentence, 80) as a function instead of an inputs command.

gregv21 -- did you put my code into your compiler?? The problem isn't that it is keeping the same string saved in the array that the user entered the first time around, its that when it loops back around it says please enter a string but then never pauses to allow the user to enter anything, and so then it just saves sentence as an empty character array... it is not going around again with the same string as last time.

when i used cin >> sentence it would only save about 3 chars of the string, which was "i hope this works".

thanks for the suggestions... i'm still haven't trouble tho, if someone could please put my code into their compiler and then go from there so they can see what i'm talking about i'd really really appreciate it.
the reason the program wont let you enter a number the second time is because it dosen't pause to allow them to enter a input so what you need to do is pause it either with
system("pause");
which leaves a nasty massage
or
cin.get();
which leaves off the first letter


if you know how to make a invisible pause or make it so the cin.get(); dosen't get rid of the first letter
Topic archived. No new replies allowed.