Please help C++ Homework (Strings)?

Home » Programming & Design » Please help C++ Homework (Strings)?
Programming & Design No Comments

Hello.
I need help with my C++ homework.

The assignment is “Write a program that for each of 5 persons has the user enter name, city, telephone number. Each of of these should be a string. In the loop after user enters these, concantenate these with spaces in between to form one string and put the resulting

Hello.
I need help with my C++ homework.

The assignment is “Write a program that for each of 5 persons has the user enter name, city, telephone number. Each of of these should be a string. In the loop after user enters these, concantenate these with spaces in between to form one string and put the resulting string into an array of size 5. Use another loop to output the array.”

I did attempt to do the assignment, but I know it’s wrong because when I compiled it, it didn’t stop after the 5th person. It continued to run and ask for the user to enter their name, city, and telephone number.

Here’s what I did:

#include<iostream>
#include<string>
using namespace std;

int main()
{
int record [5];
string name[5], city[5], tel[5],record;
for (int k=0; k<5; k++)
{
for(int m=0; m<5; m++)
{
cout<<"Enter a name"<<endl;
cin>>name[k];
cout<<"Enter city"<<endl;
cin>>city[k];
cout<<"Enter telephone"<<endl;
cin>>tel[k];
record = name[k]+ " "+ city[k] +" " +tel[k];
cout<<record<<endl;
}
cout<<record<<endl;

}

system("pause")
return 0;
}

So, please help! I’m so confused about what I did wrong. I tried to manipulate the coding and I’ve been stuck for a while. So, if anyone can explain or show me what to do, I would deeply appreciate it.
Thank you!

Best Answer:

Kris: Why do you have 2 for loops? What is the purpose of "m"?

————
const int PEOPLE = 5;
string name [PEOPLE], city [PEOPLE], tel [PEOPLE], record [PEOPLE];
for (int k=0; k<PEOPLE; k++) {
cout << "Enter a name" << endl;
cin > >name [k];
cout << "Enter city" << endl;
cin >> city [k];
cout << "Enter telephone" << endl;
cin >> tel [k];
record [k] = name [k] + " "+ city [k] + " " + tel [k];
}
for (int k=0; k<PEOPLE; k++) cout << record [k] << endl;

Other answer:

Kris:
So look at your loops
you have one inside another – for no reason I can see
The K loop K = 0
runs and M goes from 0 to 4, asking for the user data
AND then what is going to happen….
K = 1
and you repeat M from 0 to 4, asking for the user data (AGAIN !)

A good idea when debugging a program.is to add extra output / print statements
to show the value of things
If you added a 'debug' print / cout statement for K and M before each question
You would have seen that K and M were increasing as explained above… and would have solved it for yourself ?? 😉

for (int k=0; k<5; k++)
{

for(int m=0; m<5; m++)
{
*** insert a debug line here eg
cout << "Debug: k = " << k << "m = " << m << endl;
cout<<"Enter a name"<<endl;
etc…

roger:
#include<iostream>
#include<string>
using namespace std;

int main(void) {
string record [5];
string name , city , phone;
for (int k=0; k<5; k++) {
cout << "enter name:";
getline ( cin,name);
cout << "enter city: ";
getline(cin,city);
cout << "enter telephone number:";
getline(cin,phone);
record[k] = name + " "+ city +" " +phone;
}
for(int k=0;k<5;k++)
cout << record[k] << endl;
system("pause")
return 0;
}

bella:
sucks

LEAVE A COMMENT