Link to home
Start Free TrialLog in
Avatar of abel84
abel84

asked on

how do I use cin.getline()

Its for a win32 app thingy...

so basicly I want to use cin.getline()
to get the whole line...then it will split it..
example...

hello world

------
I want to be able to get hello and world in a sperate string each..I could use cin >>
but thats not part of the assignment..

so basicly I want to use getline() and then somehow split the one big char or whatever into two strings...I don't know how to do it exactly, but I appreciate any help.

Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Are you familiar with the EE homework rules (https://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/help.jsp#hi56)?
This means that I cannot give  you the final solution for your problem. You have to do this yourself, but we can help you, but you have to show us first what you already have.
Are you familiar with how getline is called? This is the first thing you have to do. Once you have the string, the second task is to split it into the different parts. YOu have to solve the first problem before you can tackle the second one.

Show us some code. You probably already have something in mind, just post it here.
Avatar of abel84
abel84

ASKER

My homework is not about the getline only...its part of the requirements to use getline()

I know how to use get line cin.getline(cin,test);

but I just don't know how to get the two values entered without using cin >>
Show us some code. Again, we are not doing your homework for you. When you call getline, what type is your "test" variable? Without talking about programming or C++, how would you split up the line if you have to do it manually? Once you know how to do it without programming, try to express your solution in programming terms (not yet in C++, just use e.g. functions and loops). And then, as a last step convert this textual description into C++ code.
Post your results here (even the textual descriptions). I will help you, but I will not do your homework for you. Show me what you have.
As khkremer said, we will not do your homework for you. We will only help you if you need clarifications. Therefore, I will not post the complete code, just the method (in english) and syntax.

I have decided to post just he syntax of getline() function: cin.getline (line, size).
Line is the string you want to extract from the user and size is the number is the number of chracters you want to extract (or until the null terminator). If you want to split it into words, search the string for a space and mark the characters extracted before (or after it). I will not give you an example or code until you can prove that you are not making us do your homework.

Regards,

Ram
Avatar of abel84

ASKER

void Prompt_Name( string& first, string& last )
{
    cout << "Please enter your full name: ";
      cin  >> first >> last;

      return;
}

thats basicly it what I have how would be able to seach for a space and extract the characters?
Hi abel84,

You've posted your code so here's mine. It basically searches the entered string for a space and marks all the characters before it (till the prevoius space: stored in the variable marked) as a word. Then it updates the the variable marked to make the currently found space character as 'previous space'.

Fairly simple code and heavily commented.

Hope you find it useful!
##############################################################################
#include <iostream.h>
#include <string.h>
#include <stdlib.h>

void main(){
      int marked=0, count=1;                              // Marked is the beginning of
      char string[100];                                    // a word | count counts the
                                                                  // number of words
      cout << "Enter a string: ";
      cin.getline(string,100,'\n');                  // Get the user's input
      cout << "\n\n\t\t\tThe Words are:\n";
      for(int i=0;i<=(strlen(string));i++){      // Uses i, an incrementing value
                                                                  // to systematically search the input
            if(string[i]==' '){                              // Searches the input for a space
                  cout << count << ". ";
                  count++;                                    // x++ means x=x+1
                  while(marked<(i+1)){
                        cout << string[marked];            // Updates marked as the character
                        marked++;                              // after the space
                  }
                  cout << endl;
            }
      }
      // The following code is meant for displaying the last word 'cos it doesn't end with a space
      cout << count << ". ";
      while (marked<i){
            cout << string[marked];
            marked++;
      }
      cout << endl << "There are " << count << " words in the string" << endl;
      system("PAUSE");
}
##############################################################################

Please tell me if you have any problems using it.

Regards,

Ram
ram_einstein, please make yourself familiar with the EE homework policy.
Avatar of abel84

ASKER

int place;
place=name.find(" ");
Break_str=strbreak.substr(0,place);

I found that to work better....anyways the points go to you ram_einstein, for at least trying to help me.
abel84, this is not about who's trying to help, this is about making sure that you are learning something. And just posting source code will not help you.
ASKER CERTIFIED SOLUTION
Avatar of ram_einstein
ram_einstein

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I see this differently. Just compare the size of the code snippet that abel84 posted and the size of the function that you posted. IMHO you just did the homework assignment, without much participation by abel84.
Avatar of abel84

ASKER

His code it too long....but I wanna use mine instead..

and this isn't just my hw assignment...its part of it...so he didn't did my himework...
I anyone would of want to do there hw for them they wouldn't of said it was hw.

anyways,

int place;
place=name.find(" ");
Break_str=strbreak.substr(0,place);

the Break_str is the first part of the string...I'm having problems of how to get the second part.
Well abel84, the simplest solution is to use the simplest code. I have written my code without any find() or strchr() or substr() functions because the challenge lies in doing it with fundamentals. Lets say for eg, you want to calculate the square root of a number. You can always use the sqrt() function in the math.h header file and be done with it (Ofcourse someone sat don and wrote the sqrt function in math.h). Or, you can device an algorithm to find the square root just using addition, subtaraction, multiplication and division.

In the same way, there MIGHT be an inbuilt function to split a string into words (I can always make a function and put it in a header file now) but we don't want to use that. So, I have built my code only using char[] to locate a place in a string and used a couple of simple loops to search for a space and print the resut. My code isn't too long. I recommend you use it. If however, you still want to use your code, post the request here and I'll see what's wrong.

Reagrds,

Ram
Avatar of abel84

ASKER

I'm just having trouble with your code...
I don't know how to assign strings to the first two words.
I don't think I quite understand what you're saying. Please clarify: "assign strings to the first two words"
Compile the code, test it. It works perfectly.

ram_einstein