So I'm looking to create a function that prompts the user to enter 'Y' for "Yes restart the program" and 'N' for No, do not restart.

I know this should use the boolean operator, I'm just unsure as to how to implement it.

I also know to encase the body in a while loop ending in "else if ('no')" for example.

I know this is completely wrong, but it's what I'm thinking could work:

bool selectRestart(int y, int n)
{
     int y = 1;
     int n = 0;
     
     cout << "Would you like to restart? [Y]es [N]o: " << endl;
                 cin >> select;
}

and the while loop being something like:

while(select == 1)
{ 
    ~
}

else if(select == 0)
{
   return 0;
}

Thanks a lot for all the help!

int main()
{
Program();
while(Restart())
{
Program();
}
return 0;
}

void Program()
{
Program code ....
}

bool Restart()
{
code to get bool .... 
}
int main()
{
Program();
while(Restart())
{
Program();
}
return 0;
}

void Program()
{
Program code ....
}

bool Restart()
{
code to get bool .... 
}

Would you be able to elaborate a bit more?

I get what you are showing, though. How I should layout the function in the program right?

The function itself, though, is where I'm having the most problem.

bool Restart()
{
     int select;
     cout << "Would you like to restart? [Y]es [N]o: " << endl;
                 cin >> select;
}

I'm not sure how to set values for 'Y' and 'N' and make it so that the program accepts them as inputs... if that makes sense?

Thanks for the help so far, it definitely helped me format the program better.

Your trying to compare letters, not numbers.

int select;//should be char or string
if(select.equals('Y'))
{//return a value}
if(no)
{//return a value}

Thanks! That helped a lot!

I got it semi-working, but there's a strange error. Here's my entire program so far (it's a string replacement program):

#include <iostream>
#include <cstring>

using namespace std;

void replaceString (string&, string&, string&);

int main()
{
    // Ask user to input a string
    // Create a function that reads a string, locates a portion, and replaces it
    // with a new string
    char select = 'Y';
    
    while(select == 'Y')
    {
   
                 string s1, s2, s3;

                 cout << "Please input a string: " << endl;
                 getline(cin,s1);
    
                 cout << "What would you like to replace? " << endl;
                 getline(cin,s2);
    
                 cout << "What would you like to replace '" << s2 << "' with? " << endl;
                 getline(cin,s3);

                 replaceString (s1, s2, s3);
                 
                      cout << "Would you like to restart? [Y]es [N]o: " << endl;
                      cin >> select;
                      }
                 
                 
      if(select == 'N')
      {
                        return 0;
                 }
    
    system("pause");
    return 0;
}
    
    
void replaceString (string& s1, string& s2, string& s3)
{
     int loc;
     int len;
     
     loc = s1.find(s2);
     len = s2.length();
     
     s1.replace(loc,len,s3);
     
     cout << s1 << endl;
     
     }

When the user inputs 'Y' at the end of the loop, the program disregards the first input (getline(cin,s1)) and goes straight to the second output. The user doesn't have a chance to input a new string.

Anyone know why this is happening? I can't seem to figure it out for the life of me. Also, any hints on how to correctly implement the 'toupper' function so that the user may input both lower and uppercase letters?

Thank you!

Thanks! That helped a lot!

I got it semi-working, but there's a strange error. Here's my entire program so far (it's a string replacement program):

#include <iostream>
#include <cstring>

using namespace std;

void replaceString (string&, string&, string&);

int main()
{
    // Ask user to input a string
    // Create a function that reads a string, locates a portion, and replaces it
    // with a new string
    char select = 'Y';
    
    while(select == 'Y')
    {
   
                 string s1, s2, s3;

                 cout << "Please input a string: " << endl;
                 getline(cin,s1);
    
                 cout << "What would you like to replace? " << endl;
                 getline(cin,s2);
    
                 cout << "What would you like to replace '" << s2 << "' with? " << endl;
                 getline(cin,s3);

                 replaceString (s1, s2, s3);
                 
                      cout << "Would you like to restart? [Y]es [N]o: " << endl;
                      cin >> select;
                      }
                 
                 
      if(select == 'N')
      {
                        return 0;
                 }
    
    system("pause");
    return 0;
}
    
    
void replaceString (string& s1, string& s2, string& s3)
{
     int loc;
     int len;
     
     loc = s1.find(s2);
     len = s2.length();
     
     s1.replace(loc,len,s3);
     
     cout << s1 << endl;
     
     }

When the user inputs 'Y' at the end of the loop, the program disregards the first input (getline(cin,s1)) and goes straight to the second output. The user doesn't have a chance to input a new string.

Anyone know why this is happening? I can't seem to figure it out for the life of me. Also, any hints on how to correctly implement the 'toupper' function so that the user may input both lower and uppercase letters?

Thank you!

It's a C++ failing not yours IMO. cin will not "grab" from the stream certain escape characters, such as \n, and it won't flush the stream after the fact either. (so when you press enter after pressing Y, the \n is still sitting in the stream). getline, on the other hand, specifically looks for \n so the first thing it sees is that left over [enter] you pressed and just grabs an empty line. To get around this you just put a getline right after cin >> select and put the \n into a dummy variable (or some other solution).


OK as for toupper... I assume you can use the built in function and you just want to use it on strings? The function works on single characters.
So since you have a string, you might do something like this (untested).

string newstring = "";
for (int i = 0; i < oldstring.size(); ++i){
  newstring += toupper(oldstring[i]);
  }

This just makes a new string and appends each character from the old string to the end of the new string, one at a time.


if you wanted to implement it from scratch, it's almost identical. Internally, the computer just knows that the data type is a char, and the value of this char is stored as a number between 0 and 255. So the letter 'A', for example, is a char with a value of 65. 'a' is 97. So basically you want to simply subtract 32 from all the values. Of course, you don't want to perform the subtraction if the character is something non-alpha, like a space in between two words. So you want to perform that check to make sure it's in the range of lower case letters before you continue.

string oldstring;
cin >> oldstring;

string newstring = "";
for (int i = 0; i < oldstring.size(); ++i){
  if (oldstring[i] < 97 || oldstring[i] > 122)
    {newstring += oldstring[i]; continue;} //if not a lower letter, copy exact, then goto begin.
  newstring += (oldstring[i] - 32);  //else change to uppper;
  }

cout << newstring;

-Greywolf

Great information!

Thank you for all the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.