Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how do I use cin.getline()

Posted on 2004-04-13
18
Medium Priority
?
1,245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 

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 90 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

618 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