4 Replies - 1394 Views - Last Post: 18 March 2013 - 08:41 PM Rate Topic: -----

#1 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

C++ input file problem.

Posted 16 March 2013 - 06:22 PM

The input file is:
tea
cup
cups
vodka
shot
shots
beer
bottle
bottles

The if statement that I used to not allow the users to input wrong things doesn't work. Can someone please help? thanks


#include "stdafx.h"



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

int main()
{
    string input_file_name;
    string beverage;
    string container_sing ;
    string container_plur ;
    ifstream input_stream;
    int choice;
    int count=1;

    cout << "Please enter the input's file name: ";
    getline(cin, input_file_name, '\n');


    input_stream.open(input_file_name.c_str());

    if(!input_stream.good())
    {
        cout << "We failed to open " << input_file_name << endl;
        cout << "Goodbye!" << endl;
        system("pause");
        return 1;
    }


cout<<"Please pick what you want for the drink, where 1 is tea, 4 is vodka, 7 is beer"<<endl;

cin>>choice;
if((choice!=1)&&(choice!=4)&&(choice!=7)){
  cout<<"Sorry you entered a bad input please try again ."<<endl;
    cin.clear();
    cin>>choice;
}

    while(count < choice && getline(input_stream, beverage ) )
    count++;
    getline(input_stream, beverage);
    getline(input_stream,container_sing);
    getline(input_stream,container_plur);


    for(int bottles=99; bottles>1; --bottles)
    {

     cout << bottles <<" "<< container_plur<<" of "<<beverage<< " on the wall," << endl;
     cout << bottles <<" "<< container_plur<<" of "<<beverage<<"," << endl;
     cout << "Take 1 down pass it around" << endl;
     cout << --bottles <<" " << container_plur<<" of " <<beverage<< " on the wall\n" << endl;
   }
     cout<< "1 " <<container_sing<<" of " <<beverage<< " on the wall,"<<endl;


     cout<< "1 " <<container_sing<< " of " <<beverage<<","<<endl;

     cout<<"Take it down and pass it around"<<endl;
     cout<<"No " <<container_plur<< " of "<<beverage<<" on the wall\n"<<endl;







     input_stream.close();

     system("pause");
     
     return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ input file problem.

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: C++ input file problem.

Posted 17 March 2013 - 05:37 AM

Is this the if statement in question?
if((choice!=1)&&(choice!=4)&&(choice!=7)){
  cout<<"Sorry you entered a bad input please try again ."<<endl;
    cin.clear();
    cin>>choice;
}


If it is you may want to think about using a while statement instead of an if.

Also even though your if statement conditions look correct sometimes it is easier to deal with positives instead of the negatives.

if(!((choice == 1) || (choice == 4) || (choice == 7))){


Jim

This post has been edited by jimblumberg: 17 March 2013 - 05:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 DanielKostinskiy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 12

Re: C++ input file problem.

Posted 18 March 2013 - 06:06 PM

Yeah it works the way you wrote it Jim. Can you explain why it doesnt work my way though? Cause I tried the same thing in Java and it works there. Thanks btw haha. Was having a great big headache with this.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: C++ input file problem.

Posted 18 March 2013 - 08:19 PM

Because with the AND all three conditions must be met, with the OR only one of the three conditions need to be met.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,449
  • Joined: 09-June 09

Re: C++ input file problem.

Posted 18 March 2013 - 08:41 PM

Quote

Yeah it works the way you wrote it Jim. Can you explain why it doesnt work my way though?


Your and jim's conditions are logically equivalent.

choice!=1 && choice!=4 && choice!=7   <==>   !(choice == 1 || choice == 4 || choice == 7)



The only difference is that a negation is factored out using Demorgan's Law, they both mean the same thing logically

This post has been edited by jjl: 18 March 2013 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1