LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2007, 03:44 PM   #1
TurtleFace
Member
 
Registered: Jul 2005
Distribution: Mandrake LE 2005
Posts: 51

Rep: Reputation: 15
reading string


could someone please tell me when the getline inside the else does not work.
Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void optimal(int len, string input) {}
void least(int len, string input) {}
void first(int len, string input) {}
void clock(int len, string input) {}

int main() {
    string input;
    char token = ' ', in[256];
    int length, other, index = 0, x = 0;
    cout << "Welcome to my Caching Algorithm Simulator.\n" << endl;
    cout << "Enter 0 for default string or 1 to enter custom stream: ";
    length = cin.get();
    cout << length << "\n\n";
    if (length == '0') {
	input = "2 3 2 1 5 2 4 5 3 2 5 2";
	}
    else {
	cout << "Enter your page address stream.\n> ";
	cin.getline ( in, 256 ); 

	}
    cout << "\nPlease choose your algorithm o(pt), l(ru), f(ifo), c(lock): ";
	cout << "\n\n\nKLFD: " << in;
    while (x == 0) {
	cin >> token;
	switch (tolower(token)) {
	    case 'o':	optimal(length, input);
			x++;
			break;

	    case 'l':	least(length, input);
			x++;
			break;

	    case 'f':	first(length, input);
			x++;
			break;

	    case 'c':	clock(length, input);
			x++;
			break;

	    default:	cout << "\n\nSorry you entered incorrect option." << endl;
			cout << "o, l, f, or c: ";
			break;
	    }
	}
    //print board
    return 0;
    }
 
Old 12-07-2007, 04:05 PM   #2
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
There are few things to consider:
1. I think you mixed up variable names 'in' and 'input'
2. When using "cin.getline( in, 256 )" how do you know how many char's are really there? Default delimeter '\n' is not written into the the variable. Might be a problem if first input of 'length' differs from number of char's in 'input[]'
3. If default string is used it ends with '\0', custom string ends with nothing.
 
Old 12-07-2007, 04:21 PM   #3
TurtleFace
Member
 
Registered: Jul 2005
Distribution: Mandrake LE 2005
Posts: 51

Original Poster
Rep: Reputation: 15
the problem im having is that when the code reaches getline it does nothing and is skipped over. i had the delimeter '\n' in before i just took it out when i was messing aroudn tryign to get it to work
 
Old 12-07-2007, 05:24 PM   #4
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
I am not sure, but try not to use name 'input' or 'in' - those might be dangerous words. Call your inptut buffer, let say, buf[256], or anything you prefer which can't be a name of some standard method or function.
 
Old 12-07-2007, 06:48 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I don't think that in or input are names of any standard C(++) function.

The first problem is when you do
Code:
 length = cin.get();
it read 1 character, but only returns after the user presses enter. This means when you call getline it just reads the newline character from before and puts an empty string into in. This is actually similar to the issue in this thread: http://www.linuxquestions.org/questi...orithm-603752/

To take care of this you can use itream's ignore method.

The second problem is that you are reading into the in buffer and displaying it (even when it is uninitialized which could cause crashes!), but you are actually using the string input as data to your functions.

I suggest you get rid of in altogether, since you can read a line directly into a string like this:
Code:
 getline(cin, input);
this also takes care of lines longer than 256 bytes.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
Reading Linux' version string TioDuke Programming 7 05-11-2006 11:43 PM
reading a char string of variable size in C introuble Programming 3 05-08-2005 01:07 PM
reading in string with fgets bostonbanana Programming 4 12-12-2004 02:10 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration