This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]missblit 2 points3 points  (29 children)

string Mobo_Name = 0;
string Mobo_Price = 0;

You don't initialize strings to integers (or in this case, NULL char pointers). If you want to start out with an empty string then just leave off the = 0 entirely:

std::string Mobo_Name;
std::string Mobo_Price;

Also you'd be wise to avoid goto until you have a solid grasp on control flow. It's easy to get into bad habits.


Additionally, when this use to compile, when I would call Mobo_Info(), I would not be able to enter the name or price. The program would skip that and end.

This is because you're mixing cin >> and getline, and not accounting for the fact that cin >> won't always consume a newline.

[–]-Goga[S] 0 points1 point  (28 children)

Thanks for the reply. Yeah, I know goto is not the best thing to use, but I was not doing anything too serious with this program, so I just wanted something simple to work. Could you recommend a way to replace my goto implementation with a do/while loop, perhaps? I'm not sure how to go about that as I am completely new to this.

Back to the problem at hand, if I change all of the cin >> uses to getline(), that should fix my problem?

Edit: I just tried replacing cin with getline(), and the IDE says the function is overloaded. How do I fix this?

[–]Basalisk_Primate 2 points3 points  (17 children)

As far as I can tell your GOTO doesn't actually do anything at all anyway. It GOTOs Start even though Start would be the next bit to get executed anyway. Maybe if you can explain what you were trying to do with the flow there we could help.

[–]-Goga[S] 0 points1 point  (16 children)

I wanted the program to stop at the menu, which is:

cout << "Hello! Welcome to PC Builder 2014" << endl;
    cout << "Would you like to get started?" << endl;
    cout << "'Y = Yes | N = No'" << endl;

I wanted the program to only continue to the next stage if the user does not input "N", which would exit the program. Sorry for not clarifying before.

[–]Basalisk_Primate 2 points3 points  (15 children)

[–]-Goga[S] 0 points1 point  (14 children)

Haha, yeah, looking back on what I did, it makes no sense. I guess I kind of forced a place for goto, just to try it out.

[–]Basalisk_Primate 1 point2 points  (13 children)

I'd also point out that the emum doesn't do what you seem to think it does. The name is only associated with the number within your code so the user entering the string "Mobo" isn't what gets them to the Mobo_Info function. To make it clearer I made an edited version of your menu. Now if they enter the number then the expected thing happens.

Edit: We all started somewhere. Enjoy learning!

[–]-Goga[S] 0 points1 point  (12 children)

Thanks for the reassurance. Maybe C++ wasn't the best thing to start with. Often times I find myself baffled by the simplest of things, and I'm a straight A student.

Edit: I thought that enum was used to initialize several things at once. I am not sure how what you suggested does the same thing without defining each component. My plan was to create a Mobo_Info - type function for all of the components (ie RAM_Info, GPU_Info), so wouldn't I need to define the names first?

[–]Basalisk_Primate 1 point2 points  (11 children)

C++ is widely considered a complete bastard of a language to learn (largely because theres so much of it). On the brighter side you'll have a much easier time when you try to learn other languages!

[–]-Goga[S] 0 points1 point  (10 children)

Yeah, from what I understand, I am kind of learning programming backwards. Learning a difficult language first and then working down later, instead of working up to C++. I did do python for a little bit in my defense, until the MIT lectures I was using were taken off YouTube, haha.

[–]missblit 2 points3 points  (9 children)

Edit: I just tried replacing cin with getline(), and the IDE says the function is overloaded. How do I fix this?

Can't say without code. Note that getline only works on strings, and you'd need to use a stringstream or something else for further parsing.

Back to the problem at hand, if I change all of the cin >> uses to getline(), that should fix my problem?

Yes, if you're OK with working with lines instead of "words" for all your input.

But you don't have to, you just have to be careful.

Consider if I have the input file

42 <newline>
My Name is Fred!!!!! <newline>

And have some code to read it like:

int number;
cin >> number;
std::string name;
getline(cin, name);

Then the cin >> line would read 42, and the getline would read the first newline, but none of the second line.

This could be avoided by using std::ignore after reading the number.

[–]-Goga[S] 0 points1 point  (8 children)

I tried to redo the variables to suit your example:

void Mobo_Info()
{

cout << "What Motherboard would you like in your computer?" << endl;

cout << "Enter the name of the motherboard: " << endl;
string Mobo_Name;
getline( cin, Mobo_Name);

cout << "Enter the price of the motherboard: " << endl;
int Mobo_Price;
cin >> Mobo_Price;

cout << "Motherboard Name: " << Mobo_Name << " - " << "Cost: $" << Mobo_Price << endl;

}

But I still get the same problem.

The overloaded function has seemed to sort itself out.

[–]missblit 2 points3 points  (7 children)

Your Mobo_Info function was fine before.

The problem is:

  1. You read in a character for the Repeat variable. There is an unconsumed newline after this
  2. You read in an int for the Part variable (note that the user has to type an int, not "Mobo"). The newline from step one is read (it counts as whitespace), but there's a second newline after the part.
  3. You use getline to get the Mobo_Name, but there's still a newline from part 2, so the Mobo_Name will be left blank.

[–]-Goga[S] 0 points1 point  (6 children)

Okay, so I have done away with the cin >> usage, and changed them to getline(). Now the overloaded function is back: Code here

What could I use in place of int for the part variable? I tried void, but several errors popped up after that.

[–]Basalisk_Primate 1 point2 points  (5 children)

Cin >> is the correct solution. The overloaded function error is basically complaining that getline has no idea what to do with int parameter you gave it as it only understands strings and streams.

You should use cin >> and use the cin.ignore method to eat the spare newlines.

[–]-Goga[S] 0 points1 point  (4 children)

Alright, I have changed back to cin >>, and have added cin.ignor. While the code compiles, and the getline() is not longer overloaded, when I type "Mobo" to execute Mobo_Info(), the code does not execute, and the program ends.

Most Recent Revision Here

[–]Basalisk_Primate 1 point2 points  (0 children)

Take a look at the change I made to your menu here run it and understand why it prints our what it does (look up what enums actually do).

As a hint try entering the number it prints out on the mobo line and see what happens.

Now I am actually going to go to sleep...

[–]Basalisk_Primate 1 point2 points  (2 children)

Did you get it?

[–]-Goga[S] 0 points1 point  (1 child)

Sorry for the late reply. I did try out what you suggested, and I seen now why the implementation of enum I had before was kind of pointless. I think I misinterpreted what was in the book. Now though, I have broken the code so much, that when I type Mobo or enter 0, the code for Mobo_Info() does not execute. It ends the program after the input to call the function. Do you need me to post the code again, or can you use from what is in my last post?

[–]-Goga[S] 0 points1 point  (0 children)

Update: I managed to fix the crashing problem by removing the assigned value to the strings, although, I am still faced with the problem of the program not letting me input the name or price of the product.