-5

I have written some code that would read from a .txt file and display whatever is in that file as output in the program. I don't know why it isn't working. I don't see any errors in the code. I also debugged the program, and all the values for the variables are correct. I am using Eclipse Luna for the IDE with the g++ compiler. When the program is running and prompts me for a number 1-3, when I type in 1 2 or 3 it doesn't do anything and I don't know why. Any insight would be appreciated. Thanks!

 #include <iostream>
#include <fstream>
using namespace std;

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

//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;

    if(x==1){
        while(objectFile >> name >> power){
            if(power==0){
                cout << name << ' ' << power << endl;
            }

    if(x==2){
        while(objectFile >> name >> power){
            if(power>0){
                cout << name << ' ' << power << endl;
            }

    if(x==3)
        while(objectFile >> name >> power){
            if(power<0){
                cout << name << ' ' << power << endl;
            }


        }
        }
    }
        }
    }
}


//main function
int main(){

    int whatTheyWant;


    whatTheyWant = getWhatTheyWant();

    while(whatTheyWant != 4){
        switch(whatTheyWant){
        case 1:
            displayItems(1);
            break;
        case 2:
            displayItems(2);
            break;
        case 3:
            displayItems(3);
            break;

        }

    }

}
shqiperia585
  • 1
  • 1
  • 1
  • 1
  • 2
    Keep debugging. The program will only do what you tell it to do, so you'll find something soon enough. – keyser Jan 02 '15 at 17:43
  • Fix your code formatting. The indentation inside `displayItems()` lies about the actual structure of the code. – Blastfurnace Jan 02 '15 at 17:46
  • 'but I don't see any errors with code?'. Yes, that is common. It's why debuggers were invented, see @keyser comment. – Martin James Jan 02 '15 at 17:58
  • Yeah, I have been debugging it for 2 days. I am following a Youtube tutorial and this code is exactly what is in the tutorial, and it works fine in the video. That's why I am here, but looks like you guys can't find anything wrong it either. @MartinJames – shqiperia585 Jan 02 '15 at 20:09

2 Answers2

0

The main issue I see is in displayItems():

while(objectFile >> name >> power) {

How does this code know where name ends and power begins?

I'd recommend having each item on a line like this:

name
power
name
power
name
power
[and so on...]

Then, you could use the getline() function to, well, get a line at a time.

Also, another big issue is you have no code to cast the string back to a double. The computer doesn't see it as "12.56." It sees it as "1" "2" "." "5" "6" (i.e. each character line by line).

This question addresses this with this code:

#include <sstream>

int main(int argc, char *argv[])
{
    double f = 0.0;

    std::stringstream ss;
    std::string s = "3.1415";

    ss << s;
    ss >> f;

    cout << f;
}
Community
  • 1
  • 1
Anonymous Penguin
  • 2,027
  • 4
  • 34
  • 49
  • I am replying to your comment where you say "How does this code know where name ends and power begins?" Exactly, I am confused by this as well. I don't think it does. But in the tutorial I followed, the code worked. Does it have to do with the IDE/compiler? The guy in the tutorial was using code::blocks. I'm using Eclipse. – shqiperia585 Jan 04 '15 at 02:47
  • @Crisa No, IDE shouldn't change code *that much*, especially because I believe this is defined behavior in C++. Can you point to your tutorial? Also, look into the "getline()" function I mentioned and then just have every item on its own line. It needs some character to differentiate data A from data B, unless a fixed width or something so you know the exact size of each item. – Anonymous Penguin Jan 04 '15 at 17:12
0

I would suggest checking out your code again, as there are some serious formatting issues hidding real issues in it. For instance, go through your code line by line and you will see that your current code only ever checks if X==1, as your other if statements are actually nested within eachother looking more like this:

if(x==1)
{
    while(objectFile >> name >> power)
    {
        if(power==0)
        {
            cout << name << ' ' << power << endl;
        }

        if(x==2){
            while(objectFile >> name >> power)
            {
                if(power>0)
                {
                    cout << name << ' ' << power << endl;
                }

            if(x==3)
                while(objectFile >> name >> power)
                {
                    if(power<0)
                    {
                        cout << name << ' ' << power << endl;
                    }
                }
            }
        }
    }
}

Do you see whats wrong with the code now? Formatting things properly will greatly enhance your ability to troubleshoot coding in the future. Also of note you go from using a:

if(X==Y)
{
//Do Stuff
}

format to using a :

if(X==Y)
//Do Stuff

format mid function. Which is generally confusing to the reader and should be avoided. Choose a format and stick to it (as a general rule). I'd be willing to bet if you troubleshoot your placement of '}'s you will find the tutorial is more likely to work for you.

Hope that helps!

BlindGarret
  • 155
  • 7
  • Thank you for your input. I don't know why when I copied the code onto this website it nested the loops. They are not nested in Eclipse. I have made a little bit of a break through though. I have changed my while loops to while(getline(objectFile, name) and it works. It outputs name but it's an infinite loop and never stops. I couldn't get it to output power (which is of type int) because getLine won't work with ints? So now I am stuck. – shqiperia585 Jan 04 '15 at 02:40
  • Well getline basically reads characters from a stream into a string until the '\n' character is hit. There is no reasons you couldn't do a cast to turn that into an int with a function turning a string into a string stream then converting from there (if you search C++ cast string to int you'll find cplusplus.com has agreat example) but I guess that's more of a style issue, as you could (with enough work) design the program so you don't need getline in the first place. Either way its not a bad thing to know how to do. – BlindGarret Jan 04 '15 at 13:08