Solved

how do I use cin.getline()

Posted on 2004-04-13
18
1,235 Views
Last Modified: 2012-05-04
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.

0
Comment
Question by:abel84
  • 5
  • 5
  • 5
18 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10818825
Are you familiar with the EE homework rules (http://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.
0
 

Author Comment

by:abel84
ID: 10820156
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 >>
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10821952
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.
0
 
LVL 2

Expert Comment

by:ram_einstein
ID: 10822223
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
0
 

Author Comment

by:abel84
ID: 10828871
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?
0
 
LVL 2

Expert Comment

by:ram_einstein
ID: 10840492
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
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10840680
ram_einstein, please make yourself familiar with the EE homework policy.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:abel84
ID: 10846662
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.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10846736
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.
0
 
LVL 2

Accepted Solution

by:
ram_einstein earned 30 total points
ID: 10847675
I have explained the source code and commented it heavily! Besides, abel84 has shown some source code which means that he knows what he is asking. Homework is telling someone to DO some assignment for you without you even knowing what the assignment is about or even being incapable to start off. abel84 has shown that. Therefore, I wanted to convince myself that I could build such a program. When I built one, I posted the source, I commented it properly and told abel84 how I built it.

Regards,

Ram
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10848621
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.
0
 

Author Comment

by:abel84
ID: 10850281
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.
0
 
LVL 2

Expert Comment

by:ram_einstein
ID: 10852119
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
0
 

Author Comment

by:abel84
ID: 10962528
I'm just having trouble with your code...
I don't know how to assign strings to the first two words.
0
 
LVL 2

Expert Comment

by:ram_einstein
ID: 10966990
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now