A nasty one

okay so basically I have to make a phone book of some sort...
When user inputs five people and their phone numbers then the computer asks to write the users last name and the computer should print out the name of the person and the number of the person.
This is some sort of practice given to me by my teacher. He said if I could make this I would understand the basics of the c++.
I should also do this program with if statements and loops.
I would really appreciate if you could give me the link to the tutorial where this kind is actually explained :D

THANKS :D
Tutorials cover how to use the syntax. This requires you to think about problem solving. There's no trick to it.

1) Get input of five people.

Well, let's see. I can get input using cin. I need to get all five, so maybe some kind of array, or a vector. The input will be names and phone numbers, so maybe an array for names and one for phone numbers. Since I need five, a loop seems sensible.

2) Get name from user.

Well, I already did some code for getting input, so I can just get another name.

3) Match that name up to the right name in the array of 5.

Hmmm. I'll use strings for names, so that I can take advantage of the string object's functions. I should read through the array of names, checking to see which one matches. That shouldn't be too hard. If they just give me the last name, maybe I can store the first and last names separately, to make this step easier.

4) Print out the right one when I find it. Well, that's easy with cout.

Bingo. There's nothing remotely difficult about any of the syntax you'll need. Really basic for loops, some strings. This is programming. Not memorising syntax. Thinking about how to solve problems.
Last edited on
ok very well put and I thank you for that :D

But the thing is I don't actually know the third step, how can I make the program read trough the last names ?

EDIT:
This is what I did so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>

using namespace std;

int main(){
    
string name[5];
string lname[5];
int tnumber[5];   

cout << "Write down the names of five people, their last names and their phone numbers. " << endl;    

for (int i = 0; i < 5; i++){
cout << "Name and Last name (Name first) ";
cin >> name[i] >> lname[i];
cout << "Phone number ";
cin >> tnumber[i];    
}


system("pause");
return 0;
}
Last edited on
This does something with every lname[i]

1
2
3
4
for (int i = 0; i < 5; i++)
{
  // do somethign with lname[i]
}
I am still trying to figure it out, but I am having a really hard time...
1
2
3
4
5
6
7
for (int i = 0; i < 5; i++)
{
  if(userInputString == lname[i])
  { 
   cout <<  name[i] << lname[i] << tnumber[i];    
  }
}
Last edited on
OMG OMG OMG OMG OMG I feel so stupid, this was so easy OMG, well you sure helped me a lot and I am really thankful :D
Topic archived. No new replies allowed.