newb question

This is a discussion on newb question within the C++ Programming forums, part of the General Programming Boards category; hey sorry if this comes off as too simple but i am trying to write a simple program and right ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    9

    newb question

    hey sorry if this comes off as too simple but i am trying to write a simple program and right now i'm only dealing with the I/O but for some reason i am having real trouble inputing a string. The code is a little messy because i have been tryign everythign i can think of to make it work but to no avail. Can someone point out my stupid mistake so i can get back to working on it. THe problem is the geline inside the else
    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;
        }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    getline for string is used like:
    Code:
    getline(cout, string);
    The version you have is meant for C style strings.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    i have tried using getline ( cin, input ); also but that did not work either. my code does not allow me to enter anythign when i reach it. that is the problem im having

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Sorry, yes, that's what I meant (cin, not cout).

    However, your problem seems to be that cin.get() leaves characters in the stream (at least newline, unless you type more than one character) which getline will happily read.

    To get rid of extra characters include <limits> and ignore the rest of the characters:
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    And use the string version of getline.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    thanks a ton it works now, it was driving me crazy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question Character Counting
    By Wilder in forum C Programming
    Replies: 13
    Last Post: 06-22-2008, 11:37 PM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  4. newb question
    By Jan79 in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2003, 09:59 AM
  5. Win app, newb question
    By Blender in forum Windows Programming
    Replies: 9
    Last Post: 02-04-2003, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21