toupper and restart loop
Nov 16, 2016 at 12:40am UTC
For this given program, how do I get name.nickname to display as uppercase? I'm going to keep my failed attempt to use toupper in there, and how to I make the program loop back to the beginning if "y" is input?
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
#include <iostream>
#include<fstream>
#include<iomanip>
#include <string>
#include <cctype>
using namespace std;
const int SIZE = 25;
struct name_components
{
string firstname;
string lastname;
string nickname;
};
name_components name;
int main()
{
{
cout << "Enter Your First Name: " ;
cin >> name.firstname;
cout << "Enter Your Last Name: " ;
cin >> name.lastname;
cout << name.firstname << " " << name.lastname << endl;
cout << "Enter You Nickname Please: " << endl;
cin >> name.nickname;
cout << name.firstname << " " << "''" << (name.nickname(), toupper) << "''" << " " << name.lastname << endl;
}
{
char repeat = 'y' ;
while (repeat == 'y' )
{
cout << "Do you want to repeat?(y/n):" ;
cin >> repeat;
}
system("pause" );
}
}
Nov 16, 2016 at 12:44am UTC
Your code does not compile. Here is the code that compiles :
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
#include <iostream>
#include<fstream>
#include<iomanip>
#include <string>
#include <cctype>
using namespace std;
const int SIZE = 25;
struct name_components
{
string firstname;
string lastname;
string nickname;
};
name_components name;
int main()
{
char repeat;
do
{
cout << "Enter Your First Name: " ;
cin >> name.firstname;
cout << "Enter Your Last Name: " ;
cin >> name.lastname;
cout << name.firstname << " " << name.lastname << endl;
cout << "Enter You Nickname Please: " << endl;
cin >> name.nickname;
cout << name.firstname << " " << "''" << " " << name.lastname << endl;
cout << "Do you want to repeat?(y/n):" ;
cin >> repeat;
} while (repeat == 'y' );
system("pause" );
}
Nov 16, 2016 at 12:55am UTC
Thank you. Now it loops, but I need the nickname to be displayed between first and last name, capitalized and in double quotes
Nov 16, 2016 at 1:55am UTC
Hi,
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
#include <iostream>
#include<fstream>
#include<iomanip>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
const int SIZE = 25;
struct name_components
{
string firstname;
string lastname;
string nickname;
};
name_components name;
int main()
{
char repeat;
do
{
cout << "Enter Your First Name: " ;
cin >> name.firstname;
cout << "Enter Your Last Name: " ;
cin >> name.lastname;
cout << name.firstname << " " << name.lastname << endl;
cout << "Enter You Nickname Please: " << endl;
cin >> name.nickname;
for (auto & x: name.nickname)
x = toupper(x);
cout << name.firstname << " " << "'" <<name.nickname<<"'" << " " << name.lastname << endl;
cout << "Do you want to repeat?(y/n):" ;
cin >> repeat;
} while (repeat == 'y' );
system("pause" );
}
Enter Your First Name: Sharper
Enter Your Last Name: C
Sharper C
Enter You Nickname Please:
than
Sharper 'THAN' C
Do you want to repeat?(y/n):n
Topic archived. No new replies allowed.