array help

Pages: 12
Hello Ive been working on a program that inputs vendor names into an array to be displayed later on and the problem I have been having is that the user has to define the number of vendors thus defining the array size. Im new to this and would love some help.
Hi,
Well, best place is at the point where you realise this isn't a homework site and showing us where you are having problems etc with work you have put in. Cut and paste the assignment doesn't count.

Everybody here is glad to help when you get off the starting blocks :)
Have you learned about vectors yet?

If not, you'll have to use the new operator.
I haven't really learned about vectors but ill look into that and see if i points me in the right direction. thanks arslan
closed account (E0p9LyTq)
I haven't really learned about vectors but ill look into that and see if i points me in the right direction.

http://www.cplusplus.com/reference/vector/vector/
I finally got a reply from my teacher and he wants me to use pointers to dynamically change the array size. Im still confused on how to do it. I wrote this according to what I have read but it only save up to 8 and displays the 8 saved names before it closes is there something im missing.

int main()
{

int n;
string *names1= nullptr;
cout << "How many vendors are there?: ";
cin >> n;
names1 = new string[n];
int l = 0;
string name2;

for (int count=0; count <n; count++)
{
cout << "Please enter the vendors name: ";
cin >> name2;

names1[l] = name2;

count++;
l++;

}

for (int time = 0; time < l; time++)
{
cout << names1[time] << endl;
}
return 0;

}
closed account (48T7M4Gy)
1
2
3
4
5
for (int count = 0; count < no_of_names; count++)
{
    cout << "Please enter the vendors name: ";
    cin >> name[count];
}


This is the sort of thing you need to do. Your code needs to be easily readable. Work backwards (and forwards) from this suggestion and tidy up the rest. Some of your variable names are too vague and should be improved accordingly.

BTW please use <> code tags from the box on the right.
> Is there something I'm missing.
Your program is good. The only thing is :
1
2
delete [] names1;
return 0;


Be sure to free your dynamically allocated array when it is nolonger needed.

Well done. Yours is ready to go!
Last edited on
Does that help? :)
Thanks Ill clean up my code so It is more understandable. When I was rewriting the code I discovered I did the whole count++ twice so that's why it only did half the names.
And remember : Be sure to free your dynamically allocated array when it is nolonger needed.

Got it?
Thanks I will. I have one other question. i write down a function to check for input validation and it works when called but it always displays enter vendor name twice. any reason why it does this?

void enternames(int k)
{
string nameCheck;

cout << "Please enter the vendors name: ";
getline(cin,nameCheck);
names1[number] = nameCheck;
while (nameCheck.empty() || nameCheck == " ")
{
cout << "Please enter the vendors name: ";
getline(cin, nameCheck);
names1[number] = nameCheck;
}
number++;

}
Last edited on
For the vendor name prompt to be displayed twice, that would imply that your while condition is true.

The only reason I can think for that to happen (assuming you enter a non-blank vendor name) is that the first getline failed or you have a \n left in the buffer from a previous cin operation. Try the following before the first getline().
1
2
  cin.clear();  //  Clear the fail bit if set
  cin.ignore (1000);  // Remove any text leftover in the input buffer 


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


 
cin.ignore(1000);


Didnt help. It made the program not work. and clear did nothing that I saw.
If that's the case, try :
1
2
cin.clear();
cin.ignore(1000, '\n'); 
That worked for the very first entry. but after that it continued to show double.
closed account (48T7M4Gy)
It's asking twice because that's what your program says. First comment out the first getline statement, and second, revisit the while condition because, without running it myself my bet is the condition is being effectively ignored. It is an unusual method of keep going on the basis of there being no input.
@nappa
So could you post your entire code again?
Here it was I have for trying to verify input and add vendors name.

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
int main()
{
	int n; 
	
	cout << "How many vendors are there?: ";
	cin >> n;
	cin.clear();
	cin.ignore(1000, '\n');

	names1 = new string[n]; // intitializes the new size of the string array with the chosen number
	
	
		for (int count = 0; count < n;)
		{
			enternames( n); // gather names
			count++;
			
		}
	
	  return 0;

}

void enternames(int k)
{
	string nameCheck;
	
		//cout << "Please enter the vendors name: ";
		//getline(cin,nameCheck);
		//names1[number] = nameCheck;
		while (nameCheck.empty() || nameCheck == " ")
		{
			cout << "Please enter the vendors name: ";
			getline(cin, nameCheck);
			names1[number] = nameCheck;
			cin.clear();
			cin.ignore(1000, '\n');
		}
			
	}
Try this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void enternames(int k)
{
	 string nameCheck;
	 do
	 {
			cout << "Please enter the vendors name: ";
			getline(cin, nameCheck);
			names1[k] = nameCheck;
		if(!cin)
		{
			cin.clear();
			cin.ignore(1000, '\n');
			nameCheck = " ";
		}
	}while (nameCheck.empty() || nameCheck == " ");
}
Pages: 12