I am using the Code: Block v10 compiler and I need help in fixing code (see inst
ID: 3626034 • Letter: I
Question
I am using the Code: Block v10 compiler and I need help in fixing code (see instructors comments below).Question
Write C++ code using Strings
Write a program that:
1) Reads in a single string entered from the keyboard in the form last_name,first_name
no embedded blanks. Note that a comma separates the two pieces. Have it store this input as a single string (NOTE THAT YOU DON’T HAVE TO READ THE STRING IN 1 CHAR AT A TIME,, BUT SHOULD READ IT IN ALL AT ONCE!)
2) NOW, break the string up and store the first_name and last_name separately as 2 separate strings.Called FIRST and LAST (Have it capitalize the first letter of each if they are not already capitalized)
3) Have it print out each together with the number of characters in each.
4) Now have your program create a new string called THE_NAME that looks like first_name last_name (Note the blank between the first and last names) Have your program print out THE_NAME
COMMENTS:
• read in the string using cin>>a string and then process it, splitting it up
• then ask the user to enter the name again and
• use cin>> to read it into an array of char and
• then split it up, using the fact that you are working with an array of char
• DO NOT USE 2 DIMENSIONAL ARRAYS
CODE
#include<iostream>
#include<sstream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string str1;
cout <<"Enter your name in the form first_name,last_name"<<endl;
cin>>str1; // OK you read in the string NOW PROCESS IT
char names[2][20]; //do not use 2 dimensional arrays, use several one dimensional arrays
string word;
stringstream stream(str1); //why are you using this??
//JUST LOOK AT THE STRING YOU ARE MAKING THIS TOO COMPLICATED
int i=0;
int count[2];
while( getline(stream, word, ',') )
{
int j;
count[i]=word.length();
for(j=0;j<word.length();j++)//why are we now in arrays
if(j==0)
names[i][j]=toupper(word.at(j));
else
names[i][j]=word.at(j);
names[i][j]='';
i++;
}
cout<<"Your first name is "<<names[0]<<" and it contains "<<count[0]<<" characters"<<endl;
cout<<"Your last name is "<<names[1]<<" and it contains "<<count[1]<<" characters"<<endl;
cout<<"Your full name is "<<names[0]<<" "<<names[1]<<endl;
return(0);
}
Explanation / Answer
please rate - thanks
appears he wants the same thing done with c-strings and strings in the program
#include<iostream>
#include<sstream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string str1,first="",last="",the_name="";
int n,m,i;
char f[10],l[10],s[21],name[21]="";
//as string
cout <<"Enter your name in the form first_name,last_name"<<endl;
cin>>str1; // OK you read in the string NOW PROCESS IT
//JUST LOOK AT THE STRING YOU ARE MAKING THIS TOO COMPLICATED
n=str1.find(",",0);
last=str1.substr(0,n);
first=str1.substr(n+1,str1.length()-n);
first[0]=toupper(first[0]);
last[0]=toupper(last[0]);
the_name=first+" "+last;
cout<<"Your first name is "<<first<<" and it contains "<<first.length()<<" characters"<<endl;
cout<<"Your last name is "<<last<<" and it contains "<<last.length()<<" characters"<<endl;
cout<<"Your full name is "<<the_name<<endl;
//as character array
cout <<"Enter your name in the form first_name,last_name"<<endl;
cin>>s; // OK you read in the string NOW PROCESS IT
for(n=0;s[n]!=',';n++)
l[n]=s[n];
l[n]='';
i=0;
for(m=n+1;s[m]!='';m++)
f[i++]=s[m];
f[i]='';
f[0]=toupper(f[0]);
l[0]=toupper(l[0]);
strcat(name,f);
strcat(name," ");
strcat(name,l);
cout<<"Your first name is "<<f<<" and it contains "<<strlen(f)<<" characters"<<endl;
cout<<"Your last name is "<<l<<" and it contains "<<strlen(l)<<" characters"<<endl;
cout<<"Your full name is "<<name<<endl;
system("pause");
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.