1

Hello Ive been working on a program where I have a file with 3 columns containing both the inauguration year and departure year of a president, along with the name of the president. Im trying to have the user input a president and have the program return the start and stop year. I began by opening the file (which opens correctly) and making 3 arrays, 2 integer arrays and one string array. The program runs but when I press 2 regardless of what name I enter the bool stays false. The file can be found here: http://pastebin.com/8h3BJxGD

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

int main()
{
char junk, x;
int start[100],stop[100],year,i,count=0;
string names[100],president;
ifstream file;
file.open("presidents.txt");


if(file.fail())
    cout<<"failed to open file"<<endl;


for(i=0;file>>start[i];i++)
    {
    file>>stop[i];
    file.get(junk);
    getline(file,names[i]);
    cout<<start[i]<<stop[i]<<names[i]<<endl;
    count++;
    }

do
{
     cout<<"What would you like to know?"<<endl;
     cout<<"Press 1 for who was President in what year"<<endl;
     cout<<"Press 2 for the years served by a President"<<endl;
     cout<<"Press 3 to stop"<<endl;
     cin>>x;

     if(x=='1')
         {
         bool valid=false;
         cout<<"Enter a year: "<<endl;
         cin>>year;

         for(i=0;i<count;i++)
             {
             if(start[i]<=year&&stop[i]>=year)
                 {
                 cout<<names[i]<<endl;
                 cout<<endl;
                 valid=true;
                 }
             }
         if(valid==false)
             {
            cout<<"Invalid year"<<endl;
             cout<<endl;
             }
         }


   if(x=='2')
         {
         bool valid=false;
         cout<<"Enter a President: "<<endl;
         cin>>president;
         getline(cin,president);

         for(i=0;i<count;i++)
             {
             if(president==names[i])  
                 {
                 cout<<start[i]<<"-"<<stop[i]<<endl;
                 cout<<endl;
                 valid=true;
                 }
             }
         if(valid==false)
             {
             cout<<"Please be more percise"<<endl;
             cout<<endl;
             }
         } 
     }
     while (x!='3');

cin>>junk;
return 0;
}   

2 Answers 2

2

Here, problem is not with the comparison, but with input string into president variable, try printing president variables value, you will understand the problem.

You need to add following line after reading x.

cin.ignore(); //add this line after cin>>x;

This will remove \n from the input buffer and will not cause any issue while reading president string. You need to take care of such issues while combining use of formatted input (i.e. >>) with unformatted input (i.e. get(), getline() etc).

Below is the modified code:

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

int main()
{
char junk, x;
int start[100],stop[100],year,i,count=0;
string names[100],president;
ifstream file;
file.open("president.txt");


if(file.fail())
    cout<<"failed to open file"<<endl;


for(i=0;file>>start[i];i++)
    {
file>>stop[i];
file.get(junk);
getline(file,names[i]);
cout<<start[i]<<stop[i]<<names[i]<<endl;
count++;
}

do
{
 cout<<"What would you like to know?"<<endl;
 cout<<"Press 1 for who was President in what year"<<endl;
 cout<<"Press 2 for the years served by a President"<<endl;
 cout<<"Press 3 to stop"<<endl;
 cin>>x;
 cin.ignore();

 if(x=='1')
     {
     bool valid=false;
     cout<<"Enter a year: "<<endl;
     cin>>year;

     for(i=0;i<count;i++)
         {
         if(start[i]<=year&&stop[i]>=year)
             {
             cout<<names[i]<<endl;
             cout<<endl;
             valid=true;
             }
         }
     if(valid==false)
         {
        cout<<"Invalid year"<<endl;
         cout<<endl;
         }
     }


  if(x=='2')
     {
     bool valid=false;
     cout<<"Enter a President: ";
     getline(cin,president);

     for(i=0;i<count;i++)
         {
         if(president==names[i])  
             {
             cout<<start[i]<<"-"<<stop[i]<<endl;
             cout<<endl;
             valid=true;
             }
         }
     if(valid==false)
         {
         cout<<"Please be more percise"<<endl;
         cout<<endl;
         }
     } 
 }
 while (x!='3');

cin>>junk;
return 0;
} 

Following is the output:

What would you like to know?
Press 1 for who was President in what year
Press 2 for the years served by a President
Press 3 to stop
2
Enter a President: Theodore Roosevelt
1901-1909
Sign up to request clarification or add additional context in comments.

7 Comments

Without the getline() how am I supposed to read the presidents into the array?
nevermind I get what you're saying. But Im pretty sure the president array is being read properly. In the first part of the program where the user enters a year and a president is returned, the proper president is outputted
you dont need to remove getline(), You just need to ignore the \n added after reading x; so after cin>>x; line just add cin.ignore();.
Problem is with this section.. if(x=='2') { bool valid=false; cout<<"Enter a President: "<<endl; cin>>president; getline(cin,president);
I added cin.ignore() and I'm still having the same issue. The second part of the program still only reads as bool=false
|
0
if(president==)

if president == what???

Also,

getline(cin,president);  //why this line?

if you have already read the file and put into data structure, then just search through the data structure for the name of the president that the user enters...if you find it, keep track of the index as you have done and print out the start[] and stop[] at the same index...

4 Comments

Just realized that, I was playing around with it trying different things. I just edited it to what it originally was
Now it says "president== ==names[i]". Please post compilable code with proper indentation.
Without the getline() the loop continues for 6-8 cycles. I'm not too sure why it does this to be honest. It also still returns false. cin>>president; getline(cin,president); seems to be the only thing that stops the loop from continuing
I'm pretty sure it is. The rest of the program works great and I use the same for loop structure in the "while(x=='1')" part of the program to sift through those arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.