-3

I want to make a program that scan from the user the number of lines of input like :

Input:

3
zyad sabry 322
ahmed alaa 11
john adams 122

Output:

zyad sabry 322
ahmed alaa 11
john adams 122

This is my code :

      scanf("%d",&n);
    for(i=0;i<n;i++)
        cin >> name >> lname >> num ;

  for(i=0;i<n;i++)
        cout << name << " " << lname << " " << num  << endl;
        return 0;
Zyad Sabry
  • 1
  • 1
  • 3
  • 1
    Please tell me that's not actually what your code looks like. – Jonathon Reinhart Mar 14 '15 at 17:51
  • I am begginer Sorry and i don't know how to post on this forum or to write – Zyad Sabry Mar 14 '15 at 17:52
  • The code @JonathonReinhart So please help me :D – Zyad Sabry Mar 14 '15 at 17:52
  • If you want to take multiple lines of input in one go then you should try the method laid out in this answer http://stackoverflow.com/questions/4006581/how-to-make-user-input-multiline-string-data-in-c and save each to a vector (easiest method for variable sized arrays) – Kvothe Mar 14 '15 at 18:01

1 Answers1

3

So you have a bunch of properties, name, lname and num, and you want to store all of them.

To store multiple objects as a collection, the std::vector is usually a good container. You can use it like this to solve your problem:

std::vector<std::string> names;  // this vector contains each name
std::vector<std::string> lnames; // this vector contains each lname
std::vector<std::string> nums;   // this vector contains each num

for(i=0;i<n;i++)
{
    std::string name, lname, num;
    std::cin >> name >> lname >> num;
    names.push_back(name);   // adds `name` to the `names` vector
    lnames.push_back(lname); // adds `lname` to the `lnames` vector
    nums.push_back(num);     // adds `num` to the `nums` vector
}

for(i=0;i<n;i++)
{
    std::cout << names.at(i) << " ";  // prints the name at index i in `names`
    std::cout << lnames.at(i) << " "; // prints the corresponding lname in `lnames`
    std::cout << nums.at(i) << std::endl; // prints the corresponding num in `nums`
}

This is not the best solution however. Since your name, lname and num variables essentially represent a person, it would be good to define a class to hold all the data of a single person. The class could be called Person, for example:

class Person
{
public:
    std::string name;
    std::string lname;
    int num;
};

Now that we have our Person class we can simply define a single vector to hold all our Person instances:

std::vector<Person> persons;

for(i=0;i<n;i++)
{
    Person person; // creates a new Person object

    // now let's read the name, lname and num for that person:
    std::cin >> person.name;
    std::cin >> person.lname;
    std::cin >> person.num;

    // and add the person to the list of persons
    persons.push_back(person); 
}

To print out the persons from the vector, we can now simply do this:

for(i=0;i<n;i++)
{
    std::cout << persons.at(i).name << " " << persons.at(i).lname << " " << persons.at(i).num << std::endl;
}

This could be of course improved even further but I'll let you learn it yourself.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157