C++: Looping Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HypeBeast McStreetwear
    New Member
    • Sep 2008
    • 20

    C++: Looping Issue

    Hello,
    I was wondering if anyone could give me a hand. I need to write a program that helps process applications for membership in a Club. Its supposed to read in information about applicants and output a statement of acceptance for each applicant.

    This is program I am using:

    [code=cpp]#include <iostream>
    #include <fstream>

    using namespace std;

    int main (){
    int num, age, social, art, income;
    int dec;
    int tievote;
    dec = 0;
    tievote = 0;
    char parents;
    ofstream results;
    results.open("/Users/putyoursoxon/Documents/results.txt");//application results go here
    cout << "Enter Canidate ID# 666 to end program" << endl;

    while (num != 666){
    cout << "Input canidate's ID #" << endl;
    cin >> num;
    //If the user inputs 666 here, they are forced to enter unnecessary
    //data.
    cout << "Input canidate's age" << endl;
    cin >> age;
    cout << "Input canidate's social skills test score" << endl;
    cin >> social;
    cout << "Input canidate's art and history test score" << endl;
    cin >> art;
    cout << "Input canidate's income" << endl;
    cin >> income;
    cout <<"Are canidate's parents in the WTS?" << endl;
    cin >> parents;
    results << "Canidate ID#" << num << ", Age =" << age << ", Social skils test score =" << social << endl;
    results << "Art and history test score =" << art << ", Income =$" << income << ", Parents in WTS? =" << parents << endl;
    results << endl;
    //display input data

    //So... You're telling the user that the data they inputted is
    //invalid... inside a file? How does that make sense?
    if ( age < 0 || age > 120){
    results << "The age of canidate ID#" << num << " is invalid" << endl;
    }
    if (social < 0 || social > 100){
    results << "The social skills test score of canidate ID#" << num << " is invaid" << endl;
    }
    if ( art < 0 || art > 100){
    results << "The art and history test score of canidate ID#" << num << " is invalid" << endl;
    }
    if (income < 5000 || income > 9999999){
    results << "The yearly income of canidate ID#" << num << " is invalid" << endl;
    }
    if (parents != 'y' && parents != 'n'){
    results << "The input for status of canidate ID#" << num << " parents is not a valid input" << endl;
    }
    //You continue to run the loop even though it's possible that one value
    //was invalid. Does this have any point?


    //Checks that the input data is within the range required
    if ( age < 30){
    results << "Canidate ID#" << num << " is not old enough to apply. Please reapply in " << 30 - age << " years" << endl;
    }
    if ( social < 60){
    results << "Canidate ID#" << num << " has failed the social skills test and cannot apply" << endl;
    }
    if ( art < 60){
    results << "Canidate ID#" << num << " has failed the art and history test and cannot apply" << endl;
    }
    if ( ((social + art) / 2) >= 90)
    dec ++;
    if ( (income / 1000) > (age * 2))
    dec ++;
    if (parents = 'y' && social >= 85)
    dec ++;
    if ( parents != 'y' || social < 85 || age < 35 && income > 200000 )
    dec ++;
    }
    //Bad structure. This should be an if like this:
    /*
    if (dec>2)
    results << "Canidate ID#" << num << " has been accepted into the Windsor Tea Society with " << dec << " votes. Congratulations !" << endl;
    else if (dec<2)
    results << "Canidate ID#" << num << " has not been accepted into the Winsor Tea Society with only " << dec << " votes. Sorry :-(" << endl;
    else
    result <<"Tie. wtf?"<<std::end l;
    */
    switch ( dec ){
    case 0:
    results << "Canidate ID#" << num << " has not been accepted into the Winsor Tea Society with only " << dec << " votes. Sorry :-(" << endl;
    break;
    case 1:
    results << "Canidate ID#" << num << " has not been accepted into the Winsor Tea Society with only " << dec << " votes. Sorry :-(" << endl;
    break;
    case 3:
    results << "Canidate ID#" << num << " has been accepted into the Windsor Tea Society with " << dec << " votes. Congratulations !" << endl;
    break;
    case 4:
    results << "Canidate ID#" << num << " has been accepted into the Windsor Tea Society with " << dec << " votes. Congratulations !" << endl;
    break;

    }
    results.close() ;
    //No return 0. What kind of compiler let's you get away with this?
    }[/code]

    The one problem I keep getting is:



    Also anyone know how I can use a loop format?

    Thanks in advance.
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    That error message means exactly what it says.
    Some languages will automatically initialize variables. C++ won't. So declaring a variable and then using it without first somehow assigning it a value is an error - because it's uninitialized, there's no way of knowing what value it contains.

    You should either initialize num to some value:
    Code:
    int num = 0;
    Or use a do loop instead of a while:
    Code:
    int num;
    do {
     cin >> num;
     // etc....
    } while (num != 666);

    Comment

    • HypeBeast McStreetwear
      New Member
      • Sep 2008
      • 20

      #3
      Thanks man, I'll try that out.

      Comment

      Working...