Hello programmers! I am having a function that has the user enter a name for an object, check if its valid, and return it if it is. I am getting the input via getline, but it seems that you have to press enter twice to make the input work. Here's a sample of my code: string getDescription(const string& thePrompt) { string newDescription=""; while (true) { cout << "\n"<< thePrompt+": " << endl; if (getline(cin,newDescription,'\n') && cin.get() == '\n' && (newDescription!="")) { //valid input of new name of the record and new description contains a valid value return newDescription;//return newDescription } …

Member Avatar
Member Avatar
+0 forum 2

In the last few days, I have noticed a very old thread ... resurrected ... about a non-standard C getline function. To all who may like a C readLine function ... a C function that reads in a dynamic C string of any length ... a function with a similar feel to the C++ getline function ... please feel free to use readLine.h (to access readLine) ... or readWord.h (to access readWord) These are freely available at: [For ... readLine and readWord ... Click Here](http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864)

Member Avatar
+0 forum 0

Hi all, First time poster here, I've referenced posts here on daniweb in the past and it has always been helpful, thanks! But I can't find the answer to this particular problem here on the forums. I need to write a basic string parser as part of a coding assignment here at penn state. I have completed the assignment using getc, but bonus points are offered for a solution using getline. The getline() man page ([url]http://linux.die.net/man/3/getline[/url]) requires stdio.h and stdlib.h for getline to compile. [CODE]#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { //welcome to main! //we need our input …

Member Avatar
Member Avatar
+0 forum 8

Hi, I have a txt file that contain certain data inside it. Example of the txt file: Name=John Age=20 Address=c-34, Newyork Name=Martin Age=23 Address=123, New Delhi .... .... and so on..... Now I have to process the each line get from the file and put the values into arrays. arr_name, arr_age, arr_add will be filled from the file. I have to read 1 line at a time, pass the values of arr_name, arr_age, arr_add to the other function to process it. Again back to the file processing function and read the second line, pass it to the function and similarly …

Member Avatar
Member Avatar
+0 forum 5

int numLines = 0; string unused; while (getline(users, unused)) { ++numLines; } cout << numLines << " Clients Found" << endl; //begin the search string search; string line; while(client.empty()) { cout << "Queue: "; //Display What The User Typed getline(cin,search); //Make it capital transform(search.begin(), search.end(),search.begin(), ::toupper); cout << search << endl; getline(users,line); stringstream ss(line); string line_part; //search each line int l = 0; //line variable while(l < numLines) { getline(ss, line_part, '.'); size_t pos; pos=line_part.find(search); // search if(pos!=string::npos) // string::npos is returned if string is not found { if(search == line_part) { cout << "Queue: " << search; cout << …

Member Avatar
Member Avatar
+0 forum 1

okay i have a file with data orgainzed like this in a dat file to be read: apple.0.0.end. and i want it to be read by char until it sees "." then print that, then read again repeated until the end of the line. so i can show the data like this: name? : apple noun? (0 for yes; 1 for no): 0 eatable? (0 for yes; 1 for no): 0 can this be done? how? and explain like im 5 years old for future reference.

Member Avatar
Member Avatar
+0 forum 2

Hi everyone, I am writing a program that converts numbers to words and vice-versa, like typing on a older cellphone. The user enters as many messages as he wants in numbers and/or text. The first character of the messages indicates what the remaining string is: numbers or letters. I can't seem to find a way to get that first character without errors spawning everywhere. I have tried using cin and getline. If someone could explain what I am doing wrong, that would be great. Here is what I have: #include<iostream> #include<string> #include<sstream> using namespace std; int main() { //Declare variables …

Member Avatar
Member Avatar
+0 forum 3

Hello, I need help to read entire string line with white spaces. I tried many ways but they didn't work :| If I remove `cin>> company_name;`the run jumps to `std::cout << "Enter the symbol of the company: ";` without completing reading company_name from the user! AND, If I keep `cin>> company_name;`the getline skips the first part from the string. Thanks in advance for your hep :) std::cout << "Enter the name of the company: "; cin>> company_name; //cin.clear(); std::getline (cin,company_name, '\n'); std::cout << "Enter the symbol of the company: "; std::cin >> company_symbol;

Member Avatar
Member Avatar
+0 forum 4

Perhaps this question has been asked or even has a thread written about it so I apologize if it's been asked before however I can't seem to find a solid answer online. I read the article: [Written by Narue](http://www.daniweb.com/software-development/cpp/threads/90228/flushing-the-input-stream) about using getline instead of cin when obtaining input from the user. My question however; Is it a good idea to "generally" stick with getline input, or can I use getchar when only needing a character (such as a 'y' or 'n'). Many articles talk about people having problems but no one really explains it so I can atleast make a …

Member Avatar
Member Avatar
+0 forum 2

Hey guys i am in trouble about BST Tree exercise.. i need to create an array that can take number of elements user want and the user has to enter integers but the program need to get this number in one line ... Here is code that i already wrote ; #include<iostream> #include<string> using namespace std; int main(){ int size,snumb; cin>>size; string *myArray = new string[size]; for(int i=1;i<=size;i++){ getline (cin,myArray[i]); } cin>>snumb; system("pause"); }

Member Avatar
Member Avatar
+0 forum 2

Hi There, I need to re-write any mention of .tellg() in my code as the function is not supported on the desired hardware. I have the following lines of code: getline(file,line); { std::istringstream stream(line); stream >> first >> second >> third >> forth; UINT32 file_pos = stream.tellg(); file_pos++; fifth = line.substr( file_pos , line.length() - stream.tellg() ); } As far as I can work out, tellg is used to find out where in the line the stream is, so fifth is anything between forth and the end of line. file_pos++; is to allow for a space. My questions are: Is …

Member Avatar
Member Avatar
+0 forum 4

I am having trouble getting MS Visual C++ to recognize my input for the 2nd getline I have listed. It already recognizes getline(cin, firstName) and allows for input, but won't recognize the second instance of getline. When the program gets to that line of output, it bunches the next two cout statements together like this: Tell us the name of the item you wish to buy? How many of these would you like to purchase today? The code should give the user a chance to enter the name of the item and allow for spaces, but it shows the output …

Member Avatar
Member Avatar
+0 forum 1

#include<iostream> #include<fstream> #include<string.h> using namespace std; int main() { //system("clear"); cout<<"\npls input the file where you want to read the data from\n"; string loc; getline(cin,loc); ifstream g; g.open (loc.c_str(),ios::in); string data; g>>data; cout<<"\n\n\n\n"; cout<<"\nthe data read from the file is\n"; cout<<endl<<data; return 0; } > even if fiole being read contains a full line of entries(separated by say spaces) then too output is the first word only....??

Member Avatar
Member Avatar
+0 forum 3

excuse me but can someone teach me how to get a string with space example : this author i need to get the codes on how to get that thank you

Member Avatar
Member Avatar
+0 forum 3

I have this function: void read(){ int id; string desc; string title, type; cout<<"Movie's id: "; cin>>id; cout<<"Movie's title: "; cin>>title; cout<<"Movie's description: "; getline(cin, desc, '\n'); cout<<"Movie's type: "; cin>>type; cout<<"You have typed in: " <<id <<" " <<title <<" " <<desc <<" " <<type <<".\n"; } and my problem is that in the desc field, when I must insert something, it skips over, passing to the type field, not letting me insert anything. I've tried in another project only with the `getline(cin, desc, '\n')` and it worked, it took all the line, but here it just skips the …

Member Avatar
Member Avatar
+0 forum 2

[CODE]void Load() { book *newptr,*p,*q; char id [12]; string title; string author; int year; float price; ifstream file1; head = NULL; file1.open("books.txt"); if (file1.fail()) { cout << "Error opening file.\n"; exit(1); } while (!file1.eof()) { newptr = new book; // file1.getline(id,12); getline(file1,title); getline(file1,author); getline(file1,year); file1.getline(price); //copy values from the file to the newptr newptr->id.assign (id) ; newptr->tajuk.assign (title) ; newptr->penulis.assign(author) ; newptr->tahun.assign (year) ; newptr->harga.assign(price) ; if(head == NULL) head = newptr; else { p = head; q = NULL; while(p != NULL && p->id.compare(newptr->id)>0) { q = p; p = p->next; } if(q == NULL) { newptr->next = head; …

Member Avatar
Member Avatar
+0 forum 1

hi! can someone help debug my codes? getline() works properly in my main() but if i'm putting it to another function, it ignores the first getline() w/o entering any words and goes to the 2nd getline. my program works like this: i have a menu() that lets the user to choose a category [easy-'e', ave-'a', hard-'h']. for example, if the user chose the letter 'e' it will jump to the easy(). and as soon as i entered the letter 'e', it goes straight to player 2 or the 2nd getline. the easy() consists of two getline (string answers for player …

Member Avatar
Member Avatar
+0 forum 3

Hi-- I am new to C++ and am having trouble with this program: The objective is to take input from a file and write it to output, using the '|' character as the delimiter, but displaying it using a comma (ex, if line 1 is "1000|6 1 6 5 3 2 | Paramus | NJ", the output would be "1000, 6 1 6 5 3 2, paramus, NJ". (the six random numbers are supposed to be test question responses, but that's largely irrelevant.) What i have so far is as follows: [CODE]ifstream reader; reader.open("SurveyResponse.txt"); char line[15]; int i=0; while( !reader.eof() …

Member Avatar
Member Avatar
+0 forum 1

This is a program which i am making of a quiz in which there are lifelines for helping the user solve his question. this function is to chose from whichever lifeline the user wants to chose. but my problem is that it is not inputing the string i.e not using the "cin.getline(maxy, 30, '\n' );". Please help... [CODE]void whichlifeline() { char maxy[30]; int integer; cout<<"\n\nDo you want to use any lifeline?(1 for Yes/2 for No)"<<endl; cin.getline ( maxy, 30, '\n' ); if((strcmp(maxy,"yes")) || (strcmp(maxy, "yes"))) { cout<<"Which lifeline do you want to use?\n1 Audience poll\n2 Double Dip\n3 50-50\n"; cin>>integer; if(integer==1) …

Member Avatar
Member Avatar
+0 forum 3

This is a part of my quiz program in which i ask the user to input question and 4 options and the correct option after which in new funtion i ask the quiz. but although it is asking me "option-1", its skipping the "Ques-" [CODE]int quizquestion() { cout<<"\nQues- "; cin.getline(stmt,100); cout<<"Option-1 "; cin.getline(opt1,30); cout<<"Option-2 "; cin.getline(opt2,30); cout<<"Option-3 "; cin.getline(opt3,30); cout<<"Option-4 "; cin.getline(opt4,30); cout<<"Correct Option number:"; cin>>correct; cin.ignore(numeric_limits<streamsize>::max(),'\n'); c++; return c; }[/CODE]

Member Avatar
Member Avatar
+0 forum 4

I was using getline() function and it was getting skipped. I searched through the forum and found the tutorial on how to flush the input stream but there is one thing that is still unclear to me : getline() uses newline character as a delimiter. So when a '\n' is found at the end in input stream getline() is skipped. As stated in the tutorial, formatted stream leaves a newline character in the stream and the mixed/non-formatted one consumes it. So shouldn't getline be skipped in formatted stream rather than in non-formatted one?? P.S : I would've posted in the …

Member Avatar
Member Avatar
+0 forum 3

Hey everyone, I'm new to this site. one of my classmates told me about it so I'm giving it a try. anyway, for my class I need to write a code for a payroll program. so far, this is what I've written... [CODE]//Assignment 4 //Page 218 //Question 7 #include <iostream> #include <string> using std::cout; using std::cin; using std::endl; using std::string; int main() { //Variables int hoursworked = 0; int weeksworked = 0; int daysworked = 0; int hoursleft = 0; string name = ""; //Input cout << "Enter the name of the worker please: "; getline(cin, name); cout << "Enter …

Member Avatar
Member Avatar
+0 forum 3

Hi there guys I am having trouble with a particular input in my program. If you will run my program and enter the word "IT" in Enter Course, it loops and jumps to do you wish to return to the main menu. Please use Microsoft Visual C++ 6.0 Professional Edition in testing my code to be able to see my problem/ Here is my code: (Note* the line on red font is the ones im having trouble with) [CODE]#include<iostream> #include<string> #include<windows.h> #include<fstream> #include <iomanip> #include<cctype> #include<cstdlib> #include <ctype.h> using namespace std; struct TF {int units, punits, misc;}; struct studre { …

Member Avatar
Member Avatar
+0 forum 5

Hi, i have a problem with getline function in cpp program on os x Error: main.cpp:158: error: 'getline' was not declared in this scope i read that in os x are some issues with getline() function but i am newbie and i dont know what to do. is possible to change compiler or...when i run program on ubuntu it runs ok but on mac i receive this error. Thanks for help. here is the code: [CODE=c++] do { printf("Napis cestu:"); } while ((readd = getline(&posielam, &len, stdin)) == -1); if (readd > 0) posielam[readd - 1] = '\0'; [/CODE]

Member Avatar
Member Avatar
+0 forum 5

How Would i be able to check for valid input on cin or getline heres what i have [CODE] #include <iostream> #include <string.h> using namespace std; int main() { string word; getline(cin, word); cout << word; system("PAUSE"); return EXIT_SUCCESS; } [/CODE] what i mean by valid input is either if the user enters an integer when i wanted a string or if the user enters nothing and causes weird results

Member Avatar
Member Avatar
+0 forum 13

Well I am coding a small intermediate program to help me with learning c++ , and i am wondering how to check for blank input to cin >> string heres what i have [CODE]string name; cout << "Enter You're Name: "; getline(cin, name); [/CODE] and if i just hit enter without typing any name the program just stays blank it is a console app btw

Member Avatar
Member Avatar
+0 forum 3

I am trying to drag a program originally written in Borland C version 3 into the light of the present using Visual C++ 2010. I have a curious, apparently simple, problem. The program read data from a file. I can create a noddy program that opens and reads the contents of a file. However, when I transfer that code into the program to be converted it complains about the getline function. The noddy program is [CODE] #include "stdafx.h" #include <iostream> using namespace std; #include <conio.h> #include <ctype.h> #include <direct.h> #include <stdio.h> #include <fstream> #include <string> using namespace std; int _tmain(int …

Member Avatar
Member Avatar
+0 forum 11

I can use the program fine without this code but I need to be able to take a line of text a file and read it into a variable. Here is the code causing the app crash: [CODE] string ReadLine() { getline(file, lineread); if (lineread.find_first_of("WSF_") != string::npos) { return lineread; } } [/CODE] What I need it to do is take a line in the text file and if it is not a W S F or _ it is an invalid line and my program will default to a hardcoded value for my maze. Row1 = ReadLine(); Row1 is …

Member Avatar
Member Avatar
+0 forum 10

I am using getline to read a CSV file. The line it reads looks something like "1/24/2010 6:19 1/24/2010 6:59 Merrill - Valley Farms yhy 69 Vegetation 55" The getline works but, I need to be able to calculate time days between a date the user inputs and the date here. AKA the first few characters in the line. I have tried to use assign and store them to arrays, which works great expect for sometimes the month is 1 or it could be 10, 11 or 12 and then assign does not work correctly. I have also tried strchr …

Member Avatar
Member Avatar
+0 forum 1

Hi, I'm currently a student given the task of programming a RPN calculator using C. The following code is some test code that I made to solve the issue described in the thread title. It calls a custom getline() function(while loop) which is also shown and then should take in two inputs entered separately or togther separeted by a space. Then when the "=" sign is entered it should print out the sum of the two numbers that were entered. And when q is pressed it should quit the program however currently what it does is that it continues to …

Member Avatar
Member Avatar
+0 forum 3

The End.