array with a string?

Hi, I need to create an array with a loop.

I want to ask for an input then search the array for that number, whatever number it is it will spit back out that array string. To be honest, I'm having trouble where to start. I can form the loop, but creating the array is a huge problem for me... To be honest I know my code is all wrong, but I really need guidance into how to form a string array.

1
2
3
4
5
6
7
8
9
10
11
12
char class[6];
std::cout class << "Please enter the class number: ";
std::cin >> ;
s[-1] = 'program ends';
s[0] = 'none';
s[1] = ' 1';
s[2] = ' 2';
s[3] = '3';
s[4] = '4';
s[5] = '5';

std::cout << class;


So if the input is 1 I want it to spit out what s{1} says. then I have it loop back around.
Last edited on
ok, thanks ive been reading up on it and trying to learn through trial and error. i just can't figure out how to call the array in the loop when they input a number. but going to keep grinding :)
closed account (2LzbRXSz)
1
2
std::cout class << "Please enter the class number: ";
std::cin >> ;

Those lines are incorrect. You need to put a << after cout, and input has to be stored somewhere.

Is this what you're trying to do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
    char class_[6]; // A class is something that already exists in
                   // C++, so I had to chang the name slightly.
                  // I'll include a link to classes.
    cout << "Please enter the class number: " << endl;
    for (int i = 0; i < 6; i++) // Because the size of the array is 6
    {
        cin >> class_[i];
        cin.ignore();
    }
    
    for (int foo = 0; foo < 6; foo++)
    {
        // Here you would loop through the array and compare them to whatever value with an if statement or switch statement
    }
    

}


http://www.cplusplus.com/doc/tutorial/classes/ (Sort of a bonus since it doesn't have much to do with the code)
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/reference/istream/istream/ignore/

Oh, and you could just simplify things a little by using a string instead of a char array.
Last edited on
Topic archived. No new replies allowed.