10 Replies - 635 Views - Last Post: 18 February 2013 - 10:59 PM Rate Topic: -----

#1 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 20-June 11

Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 08:52 PM

So I'm pretty new to C/C++ but have experience with a lot of Java. Basically I'm trying to make a prompt in the terminal that will take the user input and add it to a string array. I'm writing the program in C++ but will eventually have to use the c_str() function to generate a c-style array of chars.

Anyway, before all that, I'm having a problem with one little thing. The prompt for user input is going to be issued until "exit" is typed. If nothing is typed and just enter is pressed I want it to go to the new line and issue "prompt:" again but I'm not sure on the right way to do that. This way does not work and seems to enter some sort of infinite loop.

	
string input;

cout << "[ prompt: ] ";

    while (input != "exit")
    {
        getline(cin, input);
    
        if (input == "\n")
        {
            cout << "[ prompt: ] ";
        }
    }


This post has been edited by cthomas1489: 18 February 2013 - 08:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating basic prompt and adding user input to string array

#2 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 09:15 PM

Quote

The prompt for user input is going to be issued until "exit" is typed.

This doesn't make sense.
It seems that you don't need lines 10 - 13 if you include something like
&& input > 0 
in the while test statement.
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 09:17 PM

getline will not extract the '\n' character into the input string. Since you are just looking to prompt if the user does not "exit", you can just move your prompt to the first thing in your loop.

string input;
while (input != "exit") {
   cout << "[ prompt: ] ";
   getline(cin, input);
}



Quote

It seems that you don't need lines 10 - 13 if you include something like

input is a c++ string, relating it to an integer does not make sense, and will probably result in a syntax error

This post has been edited by jjl: 18 February 2013 - 09:20 PM

Was This Post Helpful? 1
  • +
  • -

#4 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 20-June 11

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 09:19 PM

Thanks!
Was This Post Helpful? 0
  • +
  • -

#5 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 20-June 11

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 10:15 PM

Having a little trouble breaking up my converted c+= string to cstyle char array into separate words. I'm using strtok() function. I know it's working right up until the second while loop, last statement where I'm trying to move to the next pointer. I'm getting error: 'null' was not declared in this scope. If I print right after the first strtok() call it works. I want it to print each word I time on a new line.

int main()
{

    char cstring[100];
    char *nextWordPtr;
    string input;
	
    while (input != "exit")
    {

        cout << "[ prompt: ] ";
        getline(cin, input);

        strcpy(cstring, input.c_str());

        nextWordPtr = strtok(cstring, " ");
        printf("%s\n",nextWordPtr);

        while (nextWordPtr != null)
        {
            printf("%s\n",nextWordPtr);
            nextWordPtr = strtok(0, " ");
        }
    }

    cout << endl;
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,559
  • Joined: 25-December 09

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 10:21 PM

Why are you converting this to a C-string? The string class has excellent methods of stripping words from sentences. Look up the documentation of stringstream.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 20-June 11

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 10:29 PM

Homework assignment and needs to be cstring.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4071
  • View blog
  • Posts: 12,559
  • Joined: 25-December 09

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 10:34 PM

Then stick with the C-stings and avoid the possible buffer overflow in your strcpy() call.

Remember a std::string isn't limited to a certain size like a C-string. Also there is a getline() function that works with C-strings.

Jim

This post has been edited by jimblumberg: 18 February 2013 - 10:35 PM

Was This Post Helpful? 0
  • +
  • -

#9 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 20-June 11

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 10:51 PM

Hmm all right I understand that it would be better off being solely a c-string. I believe my professor wants it done this way as we are to do things with the c++ string as well. In either case, I seem to be running into the same error if I read the line in as a cstring...

cin.getline(cstring, 100);



when I try to use strtok to cycle through the line using " " as delimiter, it is saying error: 'null' was not declared in this scope. I tried 0 or NULL, doesn't really matter in this case I don't think, but I'm not sure how to advance the nextWordPtr in the while loop.
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 10:55 PM

Quote

I'm getting error: 'null' was not declared in this scope.

null vs NULL (see the difference?)
Was This Post Helpful? 0
  • +
  • -

#11 cthomas1489  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 103
  • Joined: 20-June 11

Re: Creating basic prompt and adding user input to string array

Posted 18 February 2013 - 10:59 PM

Oooh I just smacked myself. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1