String Arrays

I am struggling to get my head around this problem.

I want to prompt for say three responses (let's say three responses are First load, Second load and Third and last load) and point these into an array. I then want to print out the array on the screen. Is the nature of the array that is my problem. If I define it as:
string array[i]
it only prints out a word, not the whole string. Presumably I need to set the length of the string using char[25],say, but how do I put this into an array of 1 to say 10 items. Can someone give me an example?

Thank you.

Last edited on
You don't really give enough information for us to help you.

A couple of comments though.
1) You can't dimension an array of strings using a variable. In standard C++ array, dimensions must be known at compile time.
2) How are you prompting for input? If you're doing cin >> array[0], be aware that cin stops at the first whitespace. That's fine if you want to input a single word, but if you're trying to input a phrase, you need to use getline.

Post some code and we can make more helpful suggestions.
Thanks for responding. I'm not near my computer at the moment (sending this from my tablet) so I can't send you the code I've produced until later. I wasn't aware of the restriction on cin (which I am using for input). This would definitely be a problem to me, so I need to read up on getline.

All dimensions will be input by the user but will be known at compile time.

I'll come back with code (successful or otherwise) after I get back to my computer.

Thanks for your help so far.
Back to my computer!

I've amended the code as below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
int i =0, c;
string name[i];

for (i=1;i<=3;i++)
   cout << "ENTER FULL NAME: ";
   getline(cin, name[i]);
   cout << name[i] <<"\n";

   return 0;
}


This prints out the following:

ENTER FULL NAME: ENTER FULL NAME: ENTER FULL NAME:

I enter the name. Joe Bloggs and press Enter. It prints Joe Bloggs once and then a Message Box comes up stating that the Filename.exe has stopped working. I close the file and the following is displayed on the console:

Process returned -1073741819 (0xC0000005) execution time : 31.055 s
Press any key to continue.


Any help will be appreciated as to why "ENTER FULL NAME: is displayed immediately 3 times and only accepts the input once. I've tried putting the code in braces {} and this only displays "ENTER FULL NAME: " once, but then the Message Box comes up immediately and the following is displayed:

Process returned -1073741819 (0xC0000005) execution time : 6.173 s
Press any key to continue.
Line 10: You initialized i to 0. How big do you think your array is? Your compiler should have flagged this as an illegal dimension.

Lines 13-15: If you want all three lines to be under control of the for loop, you need to include the 3 statements in {}. As you have it, only line 13 is under control of the for loop. This is why it displays 3 times.

Lines 12,14,15: Arrays in C/C++ are from 0 to N-1. Your loop is from 1-3. That going to cause an out of bounds reference (ignoring the problem of the zero length array allocation).
I am using CodeBlocks with GCC (g++) compiler , using the default -c switch.

The problem appears to have been associated with initializing i to 0. At a guess, I would say that this results in an out of subscript error. I don't think it's anything to do with the fact that C/C++ start arrays from 0. Providing you have enough subscripts you can use any range you want. I have a very good reason to start my arrays from 1.

The code below works. However, I'm happy to hear of any improvements that can be made.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Enter names into array and print out to console

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int i = 4;

   string name[i];

   for (i=1;i<4;i++)
   {
      cout << "ENTER FULL NAME: ";
      getline(cin, name[i]);
   }

   for (i=1;i<4;i++)
      cout << name[i] << "\n";

   return 0;
}


I'll be interested to see how it works when incorporated into the program I am writing.

@AbstractionAnon,
Thank you very much for your help.
And remember in C++ array sizes must be compile time constants.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    int i = 4;
   const int Array_Size = 4;
   string name[4];

   for (i=0;i<Array_Size;i++)
   {
      cout << "ENTER FULL NAME: ";
      getline(cin, name[i]);
   }

   for (i=0;i<Array_size;i++)


Also arrays start at zero and end at size - 1.

@jlb
I have a scenario where the array size will change and the user will input a number, which will dictate the array size. For example, when designing beams, the number of loads will vary depending on the circumstance. The user will input the number of loads and this will then dictate the size of the array. How would I deal with this dynamic array in C++?
How would I deal with this dynamic array in C++?

The best way would be to use a std::vector instead of the raw array, that is what a vector was designed to do.
@jlb
Thanks for your help. Now to read up on vectors!
Topic archived. No new replies allowed.