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 >>
Main Topics
Browse All TopicsIts 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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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
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
cout << "\n\n\t\t\tThe Words are:\n";
for(int i=0;i<=(strlen(string));i+
// 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
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
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(
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
Business Accounts
Answer for Membership
by: khkremerPosted on 2004-04-13 at 16:55:13ID: 10818825
Are you familiar with the EE homework rules (http://www.experts-exchan ge.com/Pro gramming/ P rogramming _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.