T.J.
T.J.

Reputation: 1506

input operation

Consider this problem
User will enter input to one dimensional array(char test[100]) in sequence of
letter/space/index/space/letter....
like this
a 1 s 1 e 3 r 4 r 3 t 3 until an 'enter'is hit.
Index can be repeated as well as letter.
So above sequence should mean test[1]=a ,test[1]=s and so on.
To build this problem what I have thought is that I should test whether character entered is newline(enter) or not.
but i don't understand how to do that

can you suggest some code for this

Upvotes: 0

Views: 81

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 476940

Scrap the entire idea; your question is wrong. The user does not enter a char[100]. Rather, the user enters a string. And suddenly it's very easy:

#include <string>
#include <iostream>

int main()
{
    std::string user_input;

    std::getline(std::cin, user_input);

    // done: now use user_input
}

Now you can iterate over the string, tokenize it, or whatever. For example:

std::istringstream iss(user_input);
char c;
int n;

while (iss >> c >> n)
{
    std::cout << "We have a pair (" << c << ", " << n << ")\n";
}

Upvotes: 1

Related Questions