array help

Pages: 12
And :
1
2
3
4
5
6
7
do
{
	cin.clear();
	cin.ignore(1000, '\n');
	cout << "How many vendors are there?: ";
	cin >> n;
} while(!cin);
And your input part is wrong.

==>
1
2
3
4
for (int count = 0; count < n; count++)
		{
			enternames( count); 
		}
(Does that help you?) :)
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

void enter_name(std::string *, int);

int main()
{
    int no_of_names;
    
    std::cout << "How many vendors are there?: ";
    std::cin >> no_of_names;
    std::string *name_list = new std::string[no_of_names];
    
    std::cin.clear();
    std::cin.ignore(1000, '\n');
    
    for (int count = 0; count < no_of_names; count++)
    {
        enter_name(name_list, count);
        std::cout << "Name no. " << count << " is " << name_list[count] << '\n';
    }
    
    delete [] name_list;
    
    return 0;
}

void enter_name(std::string *aName_list, int index)
{
    do{
        std::cout << "Please enter the vendors name: ";
        std::getline(std::cin, aName_list[index]);
    }while( aName_list[index].empty() || aName_list[index] == " ");
    
    return;
}
Last edited on
What an ideal solution you have, @kemort :)
closed account (48T7M4Gy)
Let's crack the 1000 - 2 to go - several_words_many_letters&numbers
Last edited on
Let's crack the 1000 - 1 to go - many_words_letters&numbers
closed account (48T7M4Gy)
LOL Well done! Now for 2000. Perhaps FBHSIE will be up for another session soon.
the solution you gave me @closed account causes the program to crash for me.
@kemort the solution does work but im trying to add an input verification so no empty inputs are accepted.
closed account (48T7M4Gy)
the solution does work
That was lucky.

im trying to add an input verification so no empty inputs are accepted
So what you need to do is think carefully and decide what an empty input looks like and/or what properties it might have.
Last edited on
Ive been rereading the code and the only thing I can think of is that it is considering the enter from he last name input as a empty string. Because the program checks for and does catch empty strings that i have tested but it continues to print out a double enter vendor name response for the next line
closed account (48T7M4Gy)
OK you are on the right track with empty. Now how are you going to use it?
Yes. that was the problem. I added
1
2
        cin.clear();
	cin.ignore(1000, '\n');


after each input ran from the for loop and it cleared the problem it seems. I'll post later if it completely worked with my code.
closed account (48T7M4Gy)
Keep in mind you only need those two lines when you go from a cin to a getline. ie it doesn't have to be inside the loop structure where only getline's occur.

If you are still getting repeated enter then make it a do .. while rather than a while .. do loop
> Because the program checks for and catches empty strings

Try this :
1
2
3
4
5
6
std::cout << "Please enter the vendors name: \n";
do
 {
			std::cout << "The vendors name: ";
          std::getline(std::cin, aName[index]);
 }while (aName[index].empty() || aName[index][0] == (' '));
Last edited on
the code you added did the same as before but the cin.clear(). seem to do the trick. Thanks guys for your help. Ill keep in mind all you told me and use it to help me become better at this.
So your problem is solved now?

Glad it helped :)
@nappa
I modified my earlier solution a little bit to your desire.

So you check it out :)
Topic archived. No new replies allowed.
Pages: 12