Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1099743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:46:43+00:00 2026-05-17T00:46:43+00:00

I am a CS student trying to grasp some C++ basic concepts. I am

  • 0

I am a CS student trying to grasp some C++ basic concepts.
I am trying to get the input of a user from std::cin and put it to an array.

example :

Input > ab ba cd[Entey key pressed]
then I would like the array to contain [ab][ba][cd].

So far I have :

#include <iostream>
#include <string>

int main(int argc, char** argv)
{
    std::cout << "Please give all strings seperated with white space (eg. ab ba cd) : ";
    std::string input[12];
    int i=0;

    while(std::cin >> input[i])
    {
        if(input[i].compare("\n")) break;
        i++;
    }
    //This will print contents of input[].
    for(int k = 0 ; k < 12 ; k++)
    {
        std::cout << "input[" << k << "] = " << input[k] << std::endl;
    }
    return 0;
}

But unfortunately this only stores the first string (in this example “ab”) in the first index of the array.

If I comment out the if(input[i].compare(“\n”)) break; a Segmentation fault will be produced. I guess because I exceed the memory allocated for the array and write in a place i shouldnt.

From what I understand so far std::cin will first put ab in input[0] of the array, and keep the remaining string [ba cd] in the stream, then on the next loop (after i++) [ba cd] will still be in the stream, cin will not read further from the keyboard (since something is on the stream) and it should put the string ba on input[1], etc. Please correct me if I am wrong.

Note : This is not homework. My course starts in about 1 month. Any help greatly appreciated. Thanks in advance

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:46:43+00:00Added an answer on May 17, 2026 at 12:46 am

    std::string::compare does not return a boolean value — it returns an int. This is used for sorting strings. It will return <0 if the left string is less, >0 if the right string is less, and 0 if they are the same. 0 is the same as boolean false, so your if statement is actually breaking whenever the string is NOT “\n” — which is why you break out of the loop on the first iteration.

    Also, cin will not give you the newline character — so checking for “\n” will not work. Using getline will help you here.

    This page is very useful for using cin/cout, istream/ostream, istringstream/ostringstream: http://www.cplusplus.com/reference/iostream/istream/

    I keep it in my bookmarks, and use it periodically.

    So, here’s probably what you want to do:

    #include <sstream>
    std::string line;
    getline(std::cin, line);  // get the entire line
    
    // parse each string from the line
    std::istringstream stream(line);
    for (int i=0; stream.good(); i++) {
      stream >> input[i];
    }
    

    Note that you should also try to allow more than 12 input strings without crashing, which can be done with a vector:

    // parse each string from the line
    std::vector< std::string > input;
    std::istringstream stream(line);
    while (stream.good())
    {
      std::string temp;
      stream >> temp;
      input.push_back(temp);
    }
    

    The vector will dynamically grow as you add new strings to it. It’s kind of like a “smarter” array in that sense — you don’t need to know it’s full size at the start. You can build it up a piece at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a student, and I'm trying to write and run some test code for
I'm a student programmer and I'm trying to use some of the widgets provided
I'm trying to get the rank of a certain student out of a class
I have two classes Teacher and Student . I am trying to get teacher
I am trying to use the struts iterator to write some data from a
I am trying to identify a user ID number, to grab a Student row
I am a student trying to use some of the machine learning algorithms for
I am trying to sort an array of student names alphabetically. In the array
Working to help a student with his project trying to get his Div to
I am trying to run this query SELECT * FROM Student WHERE DIFFERENCE(FirstName,'Joe')>=2 .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.