Hey guys,
I am a CS major at Bradley University and school is about to start so just to brush up on my C++ syntax I decided to make a simple program that would be used to study.

It is basically just a notecard program, where you enter the front of the card, then the back. you have to tell it if you have another card to enter after a card is finished, which is where I am having a problem.

After the first card, when prompted to find out if there is another card, it ignores whatever you say and just enters the catch segment of the try...catch statement. It only happens the first time through and I have tried everything I could think of.

Thanks in advance, here's the code:

oh yeah, there are a few portions of the code that I have put in just to try to help debug the program.

//Ok so let's try this notecard program one more time....
//8.10.10

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){

    //Variables to come:
    string front="";                    //temporarily stores the front of the notecard
    string back="";                     //temporarily stores the back of the notecard
    string response="";                 //stores whether or not the user wants to create another card
    string test1="y";                   //string to compare the user input
    string test2="n";                   //string to compare the user input
    vector<string> fronts;              //stores all of the fronts of the notecards
    vector<string> backs;               //stores all of the backs of the notecards
    bool repeat = 0;                    //used in a loop
    bool anotherCard = 1;               //used in a loop
    
    //Greeting
    cout<<"Welcome to NoteCards 0.1\n";
    
    while (anotherCard == 1){
    
        //Retrieve card
        cout<<"Enter front of the card  ";
        getline (cin, front);
        fronts.push_back(front);
        
        cout<<"Enter the back of the card  ";
        getline (cin, back);
        backs.push_back(back);
        
        //Ask for repetition
        cout<<"Do you have another card you want to add? 'y' for yes 'n' for no  ";
        try{
            getline (cin,response);
            if (test1.compare(response)==0 || test2.compare(response)==0){
                throw 1;
            }
        } catch (int e){
            while (repeat != 1){
                cout<<"please reply 'y' or 'n'  ";
                cout<<response<<endl;
                getline (cin, response);
                if (test1.compare(response)==0 || test2.compare(response)==0){
                    repeat = 1;
                }
            }
        }
        
        if (response.compare(test1)==0){
            anotherCard=1;
        }else{
            anotherCard=0;
        }
    }
    
    for(int i = 0; i < fronts.size(); i++){
        cout<<fronts.at(i);
    }
    
    for(int i = 0; i < backs.size(); i++){
        cout<<backs.at(i);
    }
    
    system("PAUSE");
    //If no repetition, ask for review of cards
    
    //Find a way to save information so input isn't needed every time
    
    //Ask to review again or exit
    
    return 0;
       
    
    
    
}

I suggest you read up on the string::compare function. It appears that you are interpreting the return value incorrectly. As a result, a valid input is considered invalid and an invalid input is considered valid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.