Input string

This morning I did the practice problem in Jumping into C++. But when I run it, everything is wrong. What happened here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;
struct phoneNumber
{
	string name;
	int number;
};
int main()
{
	phoneNumber contact;
	int num;
	cout << "Contacts\n";
	cout << "Enter the number of contacts you want: ";
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		cout << "Enter the name: ";
		cin >> contact.name;
		cout << "Enter the number for " << contact.name << ": ";
		cin >> contact.number;
	}
	cout << "Your contacts:\n";
	for (int i = 0; i < num; i++)
	{
		cout << contact.name << ": " << contact.number << endl;
	}
}
Last edited on
(1) You only have one phoneNumber object, despite what the user enters for num. If you want multiple phoneNumber objects, dynamically allocate an array of type phoneNumber and size 'num'. Better yet, use a vector.

(2) Better to change the type of 'number' to a long instead of int, phone numbers are usually 10 digits long.
Last edited on
You're overwriting contact num times, so whatever the user entered last will be stored in contact.

I think you intended to store multiple phoneNumber objects. In that case, you'll need to use an array or a vector.
http://www.cplusplus.com/reference/vector/vector/
How can I use a vector for it ?
> How can I use a vector for it ?

First read this: https://cal-linux.com/tutorials/vectors.html

Vectors have one important advantage with respect to C-style arrays: vectors can be resized during the execution of the program to accommodate any extra elements as needed


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <vector>

struct contact
{
	std::string name;
	std::string number; // string: allows phone numbers like 1-212-736-5000
};

int main()
{
        std::vector<contact> all_contacts ; // vector to hold all the contacts (initially empty)

	std::size_t num; // std::size_t: an unsigned integer type suitable for indexing into an array
	std::cout << "Contacts\nEnter the number of all_contacts you want: ";
	std::cin >> num;

        // while the number of contacts in the vector is less than num
	while( all_contacts.size() < num )
	{
	        contact this_contact ;

		std::cout << "Enter the name: ";
		std::cin >> this_contact.name; // single word, no embedded spaces

		std::cout << "Enter the number for " << this_contact.name << ": ";
		std::cin >> this_contact.number; // we will assume that it is a valid phone number

                // push_back: append this contact to the end of the vector
		all_contacts.push_back(this_contact) ; // note: the size of the vector increases by one
	}

	std::cout << "All your contacts:\n";

	// range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
	for( const contact& ct : all_contacts ) // for each contact ct in the vector all_contacts
	{
	        // print out the name and phone number
		std::cout << ct.name << ": " << ct.number << '\n' ;
	}
}
Last edited on
Sure that help me gentleguy!
But when I enter a name with a space like: Donald Trump ( I don't mean anything if you love Don ), it doesn't appear the "Enter the number for Donald Trump:", it breaks! The enter the number just appear if my names doesn't have a space like Donald
closed account (48T7M4Gy)
This is a common problem

use getline(cin, name);
instead of cin >> name;

This is because Donald Trump has a space between the first and second name so two strings are read from the >> stream instead of one and getline(etc) overcomes this.

If you use namespace std; you don't have to write std::getline(etc) which is confusing for beginners. Learn that later on :)
Last edited on
closed account (48T7M4Gy)
So now you need to modify your program with two things - 1. array, 2. getline()

Arrays are probably better for a newbie, it's up to you whether you use vectors because it is not the only STL container you can use and <maps> and others are functionally better anyway but they come a bit later for beginners if you are one.

Note also going from a cin line to a containing a getline may need stream correction lines:

1
2
cin.clear();
cin.ignore(1000, '\n');
// there are better choices than 1000 but it's good enough
Now I change it to getline ( cin, contact[i].name );. But after I entered the number of contacts, it appeared: Enter the name: Enter the number for: . What the heck is going on ?
closed account (48T7M4Gy)
Show me your latest code pls. It's also because you need to read my whole post about the extra two lines.
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < num; i++)
{
    cin.clear(); // <--
    cin.ignore(1000, '\n'); // <--

    cout << "Enter the name: ";
    getline(cin, contact.name); // <--
    cout << "Enter the number for " << contact.name << ": ";
    cin >> contact.number; // <-- modify as you did for array index i
}


You need something like this with the modification you have for arrays similar to the advice from gentleguy. You don't need vectors for what you are doing. :)
Last edited on
closed account (48T7M4Gy)
Does that help? :)

Must have, he's gone very quiet, probably rushing in to hand in the assignment?
Yeah, yeah, it works now! Yeah! Thanks a lot!
Topic archived. No new replies allowed.