Can someone look at my code for C++ tutorial #70

+4 Crisa Gazzola · January 2, 2015
Hello everyone. I tried to build Bucky's mock game menu in YouTube tutorials 69-70. I copied exactly what he put and the program still doesn't work. I can type in any number at all and it does nothing. Hitting 4 does not make the program quit, and hitting numbers 1-3 doesn't do anything either. Also the objects.txt file is included in the right directory.

 * main.cpp
 *  Created on: Dec 30, 2014
 *      Author: crisa
#include <iostream>
#include <fstream>
using namespace std;

//function prototypes
int getWhatTheyWant();
void displayItems(int x);

//main function
int main(){

    int whatTheyWant;

    whatTheyWant = getWhatTheyWant();

    while(whatTheyWant != 4){
        case 1:
        case 2:
        case 3:




//getWhatTheyWant function
int getWhatTheyWant(){

    int choice;

    cout << "\n 1 - just plain items" << endl;
    cout << "2 - helpful items" << endl;
    cout << "3 - harmful items" << endl;
    cout << "4 - quit program \n" << endl;

    cin >> choice;
    return choice;


//displayItems function
void displayItems(int x){
    ifstream objectFile("objects.txt");
    string name;
    double power;

        while(objectFile >> name >> power){
                cout << name << ' ' << power << endl;

        while(objectFile >> name >> power){
                cout << name << ' ' << power << endl;

        while(objectFile >> name >> power){
                cout << name << ' ' << power << endl;


Post a Reply


- page 2
Oldest  Newest  Rating
0 Robert Hrusecky · January 3, 2015
Check the closing curly braces on your if statements in the displayItems() function.
0 Gaston Seneza · January 7, 2015
Crisa, I am going to put it this way: loops  repeatedly execute a block of code. If you’re having trouble getting your mind around loops, try thinking about it this way: when the program reaches the brace at the end of the loop’s body it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block. 

 so every time you declare a variable, it's best you initialize it ( mostly to zero). WHENEVER the while loop condition is true, the while loop will run forever, resulting in an infinite loop. so we need to make sure that the condition isn't always true to avoid the infinite loop. Let's put it in simple math, use logic ( Binary numbers that actually computers use). 1 in logic means true , and 0 means false, looking back to the code of Lauri, the variable WhatTheyWant is initialized to zero, meaning that whenever the while loop jumps back up to the beginning to check the condition and decide whether to repeat it or not, it will find WhatTheyWant = 0; which is false here, therefore, no reason to keep running.

My Grammar is terrible I know that, English is my Third language, but I hope it made sense. If any confusion, feel free to ask me again, "I have all the time in the world". I will be happy to do whatever I can to help. It's a bit hard to put in words what I have to say, but I do try my best.

Note:  Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition. 

Good luck!
I am proud of you! Your eager to learn!
0 Gaston Seneza · January 7, 2015
And I should add that the keyword "break" in that while loop is very important. While you normally decide to exit a loop by checking the loop condition, sometimes you want to exit out of the loop early.  C++ has just the keyword for you: break.  A break statement will immediately terminate whatever loop you are in the middle of. 

In our case, since the user entered 2, which happens to be true according to the loop conditional statement, the keyword "break" will help us to exit out of the loop early. 

Suppose you have Apple iPhone, and you want to call Apple Customer support to fix some problems of your iPhone. You have three different phone numbers that you can call either one of them to reach Apple customer support. #1, #2, #3. It doesn't matter which one you call first, as long as the number is one of those numbers above. ( Conditional statement = true). Let's say you call #2, which is true ( meets condition), there will be no need to call number #1, and/or number #3.  Try to match this as "break" statement that you put in your while statement. as long as the user enters 2, ( the condition is true ), the break statement won't let the loop jump back up to the beginning to check the condition again. it will just exit the loop immediately.

Crisa, This is why for in your code, the while loop won't keep running after the user enters 2. it will just execute.

I think loops are very interesting to learn, and there's so much on they can do.
0 Dustin Lee · January 7, 2015
I would say you have some nested if statements, so the program might not work like it is supposed to. 
In displayItems, When you choose option 2 or 3, the if statements will not be hit, as they are in the 
if Statement Code Block for if(x==1).
  • 1
  • 2



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator