1

I have overloaded >> for my myString class. But, when I use cin >> temp and after that I use another cin for a string it seems that other cin s does not work like before. If you look to my code, I mean that the program does not understand y or n in the end and always is in the while loop.

this is istream function (a friend for myString class)

std::istream &operator>> (std::istream& input, myString& str) {
    char* temp = new char [1000];
    input >> temp;
    int i=0;
    int pow2=1;
    for (i; temp[i]!=NULL; i++) {       
        while(pow2<=i)
            pow2 *= 2;
    }
    delete [] str.string_;
    str.length = i;
    str.capacity = pow2;
    str.string_ = new char [pow2];

    for (int i=0; i<str.length; i++)
        str.string_[i] = temp[i];

    delete [] temp;

    return input;
}

This is main

cout << "myString Program" << endl;
    while(1) { //simple again or not while
        myString c;
        cin >> c;
        cout << c;

        string input;
        cout << "\nCountine (y/n)?";
        getline(cin, input);
        if (input[0] == 'n' || input[0] == 'N')
            break;
    }
Ramyad
  • 107
  • 1
  • 8
  • 3
    "Does not work" can mean a lot of things. Please provide more informations ! – Nbr44 Apr 02 '13 at 06:50
  • If you're always going to set the size of `temp` to 1000, why dynamically allocate it in the first place? – chris Apr 02 '13 at 06:51
  • Have you stepped through your code in a debugger? Seen that it reads all it should in your input operator? Seen what is input with the `getline` call? – Some programmer dude Apr 02 '13 at 06:52
  • 1
    Something tells me it's [this](http://stackoverflow.com/search?q=%5Bc%2B%2B%5D%20getline%20skipping). – chris Apr 02 '13 at 06:53
  • I have edited. The insertion is working correctly, but after that my program does not break while loop for 'n' input – Ramyad Apr 02 '13 at 06:53
  • More evidence toward my suspicion... – chris Apr 02 '13 at 06:54
  • @chris yes I think \n from the cin affects getline. How can I write my code better? – Ramyad Apr 02 '13 at 06:56
  • @chris also how can I understand user input size. As you said I am using max input size (1000). How can I understand it in execution? – Ramyad Apr 02 '13 at 06:57
  • @Ramyad, Assuming, nicely wrapped standard facilities are out, you'll have to read in less than all of it and resize as necessary. As for the `getline` issue, look at just about any of those questions. – chris Apr 02 '13 at 06:59
  • I've posted an answer that might be in line with what @chris suggests. From your implementation, it just looks like you want a way to find the number of characters in the user's input. My answer should help you do that and resolve the `getline` issue. – maditya Apr 02 '13 at 07:01

1 Answers1

1
std::istream &operator>> (std::istream& input, myString& str) {
    char temp[1000];
    cin.get(temp, 1000); //get all chars until (but not including) the next newline. Expects a size equal to the buffer used to store the chars.
    cin.ignore(); //ignore the next newline character

    int i=0;
    int pow2=1;
    for (i; i < strlen(tmp); i++) {       
        while(pow2<=i)
            pow2 *= 2;
    }
//...

(Answer based on Getting input from user using cin and http://www.cplusplus.com/forum/beginner/9148/)

Community
  • 1
  • 1
maditya
  • 8,626
  • 2
  • 28
  • 28
  • getline is for string class, I don't want to use it :) – Ramyad Apr 02 '13 at 07:02
  • Ok ... but you are already using it in the while loop in your main function ... ? :) – maditya Apr 02 '13 at 07:04
  • yes, that main.cpp is for all my programs. I am writing a class named myString like string class and I want it to work correctly in all situations! do you know something like ignore for cin. I mean can we ignore \n in the function? – Ramyad Apr 02 '13 at 07:06
  • Sure! http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input (just look at the accepted answer for how to use it) – maditya Apr 02 '13 at 07:08
  • Thanks! one more question, is there a way to get spaces with cin? I know I can use getline, but I don't want to use string class :D – Ramyad Apr 02 '13 at 07:21
  • Ok, based on that requirement I've edited my answer. :) You can use `cin.get()` to read all the characters until the next newline, then `cin.ignore()` to ignore the newline. My answer is based on http://stackoverflow.com/questions/2184125/getting-input-from-user-using-cin and http://www.cplusplus.com/forum/beginner/9148/ – maditya Apr 02 '13 at 07:40