I am working with arrays and so to help myself understand I think of a program I want to design using what I just learned, so here is what I want to do. I created a array named 'names'. It uses string array so the user enters in 5 names in a loop and stores them then starts another loop and prints out all 5. Here is what I have. When I run it however, it only prints out the last value entered
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string names[5];
for (int x = 0; x < 5; x++)
{
int a = 0;
cin >> names[a];
a++;
}
for (int y = 0; y < 5; y++)
{
int b = 0;
cout << names[b] << endl;
b++;
}
return 0;
}
If you go over the logic of your for loops you'll notice that you are declaring your array indexer to be 0 every iteration of your loop.
You don't need a new variable to use as an array index for those for loops anyway just use the x and y variables you're using in your for loop constraints.
1 2 3 4 5 6
int i;
int data[5];
for(i=0;i<5;i++)
{
data[i] = i+1;
}
hi
ok. so I don t think that you need integer a and b at all... you should delete line 11 13 17 and 19 and in first loop change name [a] to x and in second name[b] to y.
you have already stated x to 0 and every time go x + 1 so you don t need a and b any more try to get rid of them and compile and run it again.. see how that works for you..