-
Inputting to an array.
Hi, the following program is designed to take a string of input from the user and then put it into an array and manipulate it. For example, if they were to type 'hi', it would be translated to an array of {'h','i' }. Currently, I cannot get my program to work like this:
Code:
#include <iostream>
using namespace std;
int main()
{
int test;
int x;
int position=0;
char sample[8];
cout << "-> ";
cin >> sample[];
cout << "The First character you entered was " << sample[0] << "\n";
cout << "The second character you entered was " << sample[1] << "\n";
if(sample[2]=='h')
{
cout << "You did type the letter h";
}
return 0;
}
.
Thanks in advance for your help,
mintsmike
-
It should be:
or more accurately:
Code:
cin >> std::setw(8) >> sample;
(Oh, and you should #include <iomanip> for std::setw().)
-
You are trying to read a string. Remove the [].
Once you do that, it should compile correctly (I think). It's not quite clear what you are trying to achieve, so I'm not sure if it does what you want.
--
Mats
-
Thanks folks, all of your replies work.