2 Replies - 1404 Views - Last Post: 18 March 2013 - 05:50 AM Rate Topic: -----

#1 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

else if formatting C++

Posted 18 March 2013 - 12:14 AM

Hi all,

I am in the second phase of a project where I need to extend my program into a menu driven application to query the database I have on a .txt file. So, my trouble is that I cannot get my loop to be perpetual. It always terminates when it initializes from one option to the next. Here is the snippet of my code that is giving me trouble:

    cout << count << " records loaded successfully." << endl;
    	cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
        cin >> task;
    	if ((task == 'Q')||(task =='q'))
        {
            cout << "Program will now terminate";
            return 0;
        }
        else if ((task == 'S')||(task =='s'))
        {
           showAll (loadData (pathname));
           cout << endl;
           cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
           cin >> task;
        }
            
    	   return 0;
    	}


I need to add two more options into the loop on top of these two but I figured I should get the first two working correctly first. The other two should be plug & chug after that. Basically what I was trying to do is say if the user enters Q or q, terminate, if user hits S or s, activate showall function and after ward, go back to the original option screen. It isn't working though.

Is This A Good Question/Topic? 0
  • +

Replies To: else if formatting C++

#2 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Re: else if formatting C++

Posted 18 March 2013 - 12:52 AM

I did not see an option to edit my original post. So, I will need to post more code on a separate comment here. I am posting my whole int main for clarity since I think my while(infile) will cause an issue with my loop:
	int main ()
{
        char Q,q;
        char S,s;
        char task;
        string pathname;
        string z;
        int count=0;
        cout << "Welcome to Jason Rodriguez's Library Database." << endl;
        cout << "Please enter the name of the backup file: ";
        cin >> pathname;
        ifstream inFile(pathname.c_str());
        while(!inFile.eof())
        {
            getline(inFile,z);
            count++;
        }
       
    while (task != 'Q' || task != 'q') {
        
        cout << count << " records loaded successfully." << endl;
        cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
        cin >> task;
        if ((task == 'Q')||(task =='q'))
        {
            cout << "Program will now terminate";
            break;
        }
        else if ((task == 'S')||(task =='s'))
        {
            showAll (loadData (pathname));
            cout << endl;
            cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All: ";
            cin >> task;
        }
        
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: else if formatting C++

Posted 18 March 2013 - 05:50 AM

Your task variable in uninitialized the first time the while loop is encountered, but that is an aside and not the source of your problem. The problem is that since task is a char type, the cin extraction will only input a single character. Since at the keyboard the user types in 'S' <Enter>, the <Enter> is still sitting in the input buffer after the 'S' is extracted. You'll need to clear the input buffer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1