-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;
  • 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.

emlai
  • 39,703
  • 9
  • 98
  • 145
  • Why do you use `.at()` rather than just square brackets? – Kvothe Mar 14 '15 at 18:07
  • Guys i need something that helps you are writing difficult codes to me i want it simple . – Zyad Sabry Mar 14 '15 at 18:10
  • @Kvothe Because it warns you when you try to access out of bounds. – emlai Mar 14 '15 at 18:12
  • 1
    @ZyadSabry This is as simple as it can get. What you really need is to learn the language. Btw check the edit I made. – emlai Mar 14 '15 at 18:13
  • @ZyadSabry This is the best and simplest way to do it. What is is that you don't understand? I suggest looking up a tutorial to teach you about using vectors. – Kvothe Mar 14 '15 at 18:14
  • @zenith When you say warning, do you mean like a runtime_error? – Kvothe Mar 14 '15 at 18:15
  • @Kvothe Yes exactly that. – emlai Mar 14 '15 at 18:16
  • @ZyadSabry: [I suggest you read a good book.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) Far too many "tutorials" show outdated, awkward, or simply bad practices. – Blastfurnace Mar 14 '15 at 18:16