Reputation: 43
I want to input a string array in C++. For example:
name[]={"jun","hua","kyu",..}
cout<<name[2];
output: hua
This is my code but the string that I input doesn't stop. I don't know how to fix it. Please, help me. Thank you.
string name[50];
string st;
int i=0;
do{
cin>>st;
name[i]=st;
i++;
} while (st!="\0");
Upvotes: 1
Views: 35983
Reputation: 28178
Use a vector of strings:
std::vector<std::string> strings;
Read strings into strings
:
for(std::string s; std::cin >> s;)
strings.push_back(std::move(s));
Print out strings:
for(auto& s : strings)
std::cout << s << '\n';
Upvotes: 1
Reputation: 9843
You should put std::cin
in the condition of a while loop, this way when ever it fails (which it will when you reach a null terminating string) it will return false and it will leave the loop.
Here is an example using std::vector
#include <string>
#include <vector>
#include <iostream>
int main()
{
std::vector<std::string> userInputVec;
std::string input;
while(std::cin >> input)
{
userInputVec.push_back(input);
}
}
Upvotes: 2
Reputation: 153919
It's not clear to me what you're trying to do. If the entire
contents of the file are "kyu"
, then the input definitely
will stop when it reaches end of file. What I suspect (but I'm
really just guessing) is that you've output the strings without
a separator, and so the input doesn't know where each string
ends, and reads all of the characters as a single string. In
other words, your output file contains "junhuakyu..."
.
When outputting strings (and a lot of other things) as text, you must find some way of deliminating them. If the strings use a restricted character set (e.g. just letters), it's often sufficient to just insert a separator character between each string, choosing one which cannot appear in the strings. For example:
for ( auto p = std::begin(name); p != std::end(name); ++ p ) {
if ( p != std::begin(name) ) {
std::cout << ' ';
}
std::cout << *p;
}
This will give you something like "jun hua kyu ..."
in the
file, which can easily be read using:
std::string st;
while ( std::cin >> st ) {
// ...
}
Which brings up a secont problem with your code. You're using
the results of what you've read without testing whether the read
succeeded. If it fails (e.g. because of end of file), then st
may not have been modified; at any rate, if the file is a text
file, and really only contains text, you'll never get st ==
"\0"
; this can only occur if the file actually contains
a '\0'
character (which isn't legal if it is a text file).
Finally, of course: the choice of separator is up to you. White
space is particularly simple; if the strings can contain blanks,
you might choose '\n'
, and use std::getline
to read. And if
worse comes to worse, and the strings can be literally anything,
you'll have to implement some sort of quoting mechanism,
possibly with escape characters.
Upvotes: 1
Reputation: 35
i hope it will help uu
string name[50];
string st;
int i=0;
do{
cin>>st;
name[i]=st;
i++;
} while (st!="\n");
or use
string name[50];
string st;
int i=0;
do{
getline(cin,name[i]);
i++;
} while (i<50);
Upvotes: 0
Reputation: 2248
Do you send a null character in the input stream of your program? I guess you don't, so a better way to write your loop would be:
while ( std::cin >> st ) {
name[i] = st;
++i;
}
std::istream::operator>>()
returns the read stream. If it could not read the requested type, the stream will have a fail bit turned on and will result to false
when casted as a boolean.
Also, you should check the value of i
not to write outside name
.
Upvotes: 0
Reputation: 254471
Formatted input, cin>>st
will skip over all whitespace (including blank lines), and give you the first word it finds.
I'm guessing you want to stop when there's a blank line. You could do something like this, to read entire lines:
while (i < 50 && getline(cin, st) && !st.empty()) {
name[i] = st;
i++;
}
If you want to read individual words, and allow multiple words on a line, then parse each line after reading it:
while (i < 50 && getline(cin, st) && !st.empty()) {
std::stringstream ss(st);
std::string word;
while (i < 50 && ss >> word) {
name[i] = word;
++i;
}
}
You should consider using std::vector<string> name;
, rather than a fixed-size array, and adding names with push_back()
, to avoid the ugly range checks in my code or the possibility of horrific stack-corruption bugs in yours.
Upvotes: 1