C++ Tutorial 69

0 Vikram Jayanthi · July 16, 2014
Code below:
#include <iostream>
#include <fstream>
using namespace std;
int getoption();
void displayobjects(int a);

//Main Function
int main()
{
  int option;
  option=getoption();

while(option!=4)
{
switch(option)
{
case 1:
   displayobjects(1);
break;
case 2:
    displayobjects(2);
    break;
case 3:
    displayobjects(3);
    break;

}

option=getoption();


}



}



int getoption()
{
  int choice;
  cout<<"Enter 1 for neutral items"<<endl;
  cout<<"Enter 2 for Positive items"<<endl;
  cout<<"ENter 3 for Negative items"<<endl;
  cout<<"Enter 4 to quit!"<<endl;
cin>>choice;
return choice;

}


void displayobjects(int a)
{
ifstream file;
file.open("objects.txt");
string name;
int power;
if(a==1)
{
while(file>>name>>power)
{
 if(power==0)
 {
     cout<<name<<"   "<<power<<endl;
 }
}
}
if(a==2)
{
while(file>>name>>power)
{
if(power>0)
{
cout<<name<<"    "<<power<<endl;

}

}


}
if(a==3)
{
while(file>>name>>power)
{
if(power<0)
{
    cout<<name<<power<<endl;
}

}

}



}

When I run it no errors are shown but when I choose an option by pressing 1,2 or 3 it just keeps looping the Opening Menu.
Thanks for the help.


Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · July 17, 2014
What do you mean with "Opening Menu" ?
And could you also post whats in the file "objects.txt", please?
+1 Mathias Frits Rørvik · July 17, 2014
Could you please put your code in a code box?
  • 1

C++

106,968 followers
About

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

Links
Moderators
Bucky Roberts Administrator