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 :)
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;
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.
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.
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++;
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.
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.