### Need help with ARRAYS please!

I have the majority of this program written but I see flaw in it the last section after the do while statement i think needs to be another array probably close to the one on top but I just cant seem to get it. If anyone could help that would be great. The program asks the user several times for you to enter a positive integer up until you enter -1 to exit the loop. Then lets say you do four inputs and on the fifth prompt you enter -1. This would mean an output would show 4 integers entered, once you enter -1 the program then asks you to enter a string as many times equal to how many integers you just entered, example:

"please enter a positive integer (-1 to exit): 1
"please enter a positive integer (-1 to exit): 2
"please enter a positive integer (-1 to exit): 3
"please enter a positive integer (-1 to exit): 4
"please enter a positive integer (-1 to exit): -1

The output for this should be:

1 : : hello
2 : : to
3 : : you
4 : : guys

but instead when the code is compiled and the input stored the output is this:

1 : : guys
2 : : guys
3 : : guys
4 : : guys

The string part of this code I want to become an array so as to make work the right way but I am not sure how. Here is my code with comments.

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include using namespace std; int main() { string word; int integer[100]; int integerVar=-1; do { integerVar = integerVar + 1; if (integerVar > 100) { cout << "Highest possible amount is 100"; break; } cout << "Enter a positive integer (-1 to exit): "; cin >> integer[integerVar]; } while (integer[integerVar] != -1); cin.ignore(); // I want an array for 'word' to make the expected output just need a lil help string word[100]; int wordVar = wordVar + 1; while (word[wordVar] < integer[integerVar]) { cout << "Enter a string: "; cin >> word[wordVar]; } } ``````
Last edited on
`int wordVar = wordVar + 1;` is outside the "string loop". Do the same thing you did inside the "number loop" and it should work fine.

 cin.ignore(); // I want an array for 'word' to make the expected output just need a lil help

Didn't understand this. I don't know if you're looking for this

 ``1234`` ``````string str; cin >> str; word[wordVar] = str; ``````

The second loop should iterate as many times as the number of inputted numbers. Therefore, you need to declare a counter that keeps track of how many integers have been inputted.

Was giving me weird errors, never do this:` string string;`

This means nothing: `int wordVar = wordVar + 1;`
Last edited on
Line 27 you are declaring wordVar and using it at the same time ?

Line 28 - that comparison is meaningless...
 ``12`` ``````//note that integer[integerVar] == -1 so it's probably not the comparison you want //word[wordVar] == "some string" and not an integer ``````

 ``12345678`` ``````//untested int wordVar = -1; while (wordVar < integerVar) { wordVar = wordVar + 1; //do u mean for this inside the loop like earlier? cout << "Enter a string: "; cin >> word[wordVar]; }``````
Last edited on
ok guys I apologize for the errors I did not mean to string a string I meant string word and all I am trying to do is make answers "enter a string" show up just like they did in the first output at the top of the forum post and soranz it is possible thats what i wanted.

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include using namespace std; int main() { int a[100]; string s[100]; int count = 0; do { cout << "\nEnter a number: "; cin >> a[count++]; } while (count < 100 && a[count-1] != -1); int i; for (i = 0; i < count - 1; i++) // you will eventually enter -1 { cout << "\nEnter a word: "; cin >> s[i]; } for (i = 0; i < count - 1; i++) { cout << a[i] << " :: " << s[i] << endl; } return 0; }``````

 ``` Enter a number: 94 Enter a number: 96 Enter a number: -1 Enter a word: ninetyfour Enter a word: ninetysix 94 :: ninetyfour 96 :: ninetysix ```
Last edited on
Topic archived. No new replies allowed.