Question regarding string array

Pages: 12
so if we have

string n[5]={"jay", "mike", "paul", "billy", "adam");

and we want to output them

isn't this suppose to be right ? i want to output those names... it is not working...
for (i=0; i<5; i++)
{ n[i];
cout << n[i] << " "; }




Except that this expression statement

n[i];

has no any sense

the code is valid and ahould output elements of the array.
@vlad from moscow


what do you mean has no sense ?
closed account (Dy7SLyTq)
he means whats the point of n[i]; and i agree. it literally does nothing
what do you mean has no sense ?

You have a statement that is just:

n[i];

It doesn't do anything at all. It makes no sense to have it.

EDIT: And please use code tags when posting code.
Last edited on
string n[5]={"jay", "mike", "paul", "billy", "adam");

I think you are getting compiler error for the above statement
closed account (Dy7SLyTq)
good catch. didnt even notice that
So to recap,

Old Version :
1
2
3
4
5
6
string n[5]={"jay", "mike", "paul", "billy", "adam");


          for (i=0; i<5; i++)
       { n[i];
         cout << n[i] << " "; }  


New Version
1
2
3
4
5
string n[5]={"jay", "mike", "paul", "billy", "adam"};


          for (i=0; i<5; i++)
                cout << n[i] << "\n"; // loops through the array's index and display each string   
Although it's generally safer to put the braces around a block even when it's a single line:

1
2
3
4
5
6
7
string n[5]={"jay", "mike", "paul", "billy", "adam"};


          for (i=0; i<5; i++)
          {
                cout << n[i] << "\n"; // loops through the array's index and display each string 
          }


If I had a quid for every bug I've seen introduced into code that would have been prevented by this, I wouldn't have to keep writing C++ for a living :)
closed account (N36fSL3A)
Newer Version
1
2
3
string n[5]={"jay", "mike", "paul", "billy", "adam"};
          for (int i=0; i<5; i++)
                cout << n[i] << "\n"; // loops through the array's index and display each string    
Last edited on by Fredbill30
Newer version (just two years old):
1
2
3
const std::string names_array[] = { "jay", "mike", "paul", "billy", "adam" } ;
for( const std::string& name : names_array ) std::cout << name << '\n' ;
// see: http://www.stroustrup.com/C++11FAQ.html#for 
Let me tell you what is happening, there is data given like that

jay 44 55 66 77 88 33
mike 44 22 77 22 11 76
paul ...
billy
adam

and when I do the for loop we are all talking about, what I am getting is

jay 44 55 66 77 << the numbers are being placed in the string, i want to place the names only !

@MikeBoy

there is no compiler error i was just giving an example. but the the real thing is Data like that and i have to use ifstream, i do not want to get too much into it i just want to understand why it is not putting the names only
MikeyBoy

It's for the infile

infile >> n[i];

It's for the infile

infile >> n[i];

I'm not sure what you mean by that.

In any case, I'm telling you that, whatever you intend, the statement:

n[i];

on its own like that does nothing at all, and serves no purpose whatsoever.
Last edited on
closed account (Dy7SLyTq)
its as bad as cout<<"";
@Bleedz129
Try and rephrase your question... What EXACTLY do you want help with?
Last edited on
Okay

W have a data on text file that has something like that

jay 44 55 66 77 88 33 27
mike 44 22 77 22 11 76 78
paul ...
billy
adam

i need to take the names and store them into a string array
then take the numbers, and store them into a two dimensional integer array, i need to calculate the total numbers for each name at the end. but I am still stuck on getting the names inside the string from he first place.

here is my code

1
2
3
4
5
6
7
8
9
ifstream inFile;
string n[5];
int data[5][8]

	for(int i=0; i< 5; i++)
			{
				inFile >> n[i];
				for (int j=0; j<8; j++)
					inFile >> data[i][j];}

now if i output n[i] inside the loop, i want to see something like that
Jay mike paul billy adam
instead this is what I am getting
jay 44 55 66 77

so numbers are taken as string you know....
I hope this clarify everything

Last edited on
closed account (Dy7SLyTq)
i get you. i have a solution
closed account (Dy7SLyTq)
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
	ifstream file("whatever.txt");
	string names[5], line;
	int numbers[5][8], counter = 0;

	while(getline(file, line))
	{
		istringstream stream(line)

		cin>> names[counter];

		for(int j = 0; j < 8; j++)
			cin>> numbers[counter][j]

		counter++;
	}
}


edit: not the most elegant thing imo but it should work. dont have time to test it however so let me know if there was an issue
Last edited on
thanks @DTScode
but i do not know the purpose of these
1
2
3
getline(file, line))
	{
		istringstream stream(line)


and these int argc, char *argv[]) never learned them and

i do not want to use something i never learned... that would look fishy
is there anyway I can fix my code ? i do not mind changing it to while loop..
Last edited on
Pages: 12