So, I'm trying to do something simple: get a line into a string, and I have this: getline (ins, temp);
But, it never seems to stop, in that I hit enter as many times as I want and get nothing. Why? Thanks in advance!
ins is an input stream, so that wouldn't really be helpful. I did try that though, and it did the same thing as before. This is embedded in a function inside of a class, I can post the entire function if it would be helpful, not that it gets to the rest of the function.
Yes, I enter three letters, each followed by a space. This is part of the extraction operator (>>) for a class that describes a resistor. The idea is to read in the line, determine how many spaces are present, if none, then the user entered just the numeric value, so I would then convert it to an integer, if two then I use a switch statement to assign the colors of the resistor. Here is the code:
I stopped at this point to see if my logic for determining number of spaces was correct, but I'm never seeing here printed to the screen. I'm sure its something simple.
Yes to the first two. I'm using an IDE called Code::Blocks, which using GCC. I have considered it, but I have a hard time believing in anything being noticeably wrong with GCC, though I'll look into it. Ahhh, yes, you are right, not that I think that is affecting it...I probably would have run into that later lol. Thanks for pointing it out, probably would have been a headache later!
Do you use the GCC version that's being shipped with Code::Blocks? I am not sure, but it might be an outdated one. Oh, and what OS are you using? Windows? Then are you using MinGW or Cygwin GCC?
Code::Blocks says GCC 4.3.2 32-bit, and I'm using Code::Blocks 10.05. I'm running an Ubuntu based Linux distro called UberStudent. I can post the entire class if it would be helpful.
Oh, I just discovered that if I type only one letter followed by a space and then hit enter, it works. It must be one letter only, followed by a space.
Well, that's interesting. I was just adding comments, so I recompiled, and what do you know it works...sort of. If you type "a b c" it doesn't work, but if you type "a b c " it does, it relies on that last space.. Strange that I tried that before and it didn't work!
I'll go ahead and change it since I think that is the more accepted way, but it doesn't infinite loop, and it did return the right value. I'll leave this open for a day to see if anybody can figure out why the space is necessary for it to work and then I'll mark as solved.
Assuming that the colours array has the right dimension
1 2 3
//if you already passed by an space
temp2 = temp.substr(pos, temp.find(' ', pos) - pos - 1); //second argument is -1 (subtract all you can)
pos = temp.find(' ', pos); //pos is not changing
So temp2 = " violet orange", maybe that is causing issues in Resistor::int_equ or later on Resistor::convert_to_int
Good point, but the problem is that it never gets there, I put a cout right after the getline, and the line entered is never outputted, it just returns Segmentation Fault.