C++ Looping/Inputting data (fstream)

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

    C++ Looping/Inputting data (fstream)

    Hey Guys

    I have to make a program where it accepts/denies applications into a club. The program should read in information about applicants (None was given so I believe I have to make up some applicant info in Notepad) and output a statement of acceptance or rejection for each applicant.

    Each input line contains the follwing information about the applicant:
    • Applicant's ID number (0-9,999)
      Age (0-120)
      Score on the Test of Social Skills (0-100)
      Score on the literature Art and History test (0-100)
      Yearly Income (5,000 - 9,999,999)
      An indication of whether the applicants parents where already in the club (y/n)


    There are 4 judges and each Judge vote for different things:
    • Judge 1 votes to accept any candidate who averages 90 or better on the 2 tests.
      Judge 2 votes to accept any candidate whose income, in thousands of dollars, exceeds twice his or her age.
      Judge 3 votes to accept any candidate who scores at least an 85 on the social skills test and who parents have been inthe Club.
      Judge 4 always disagrees with Judge 3 with the exception that she always approves an applicant who is under 35 years old and earns more thank $200,000.


    In the event of a tie the judges have to take a second vote. At that point judge 3 lower her social skills test requirement down to 75 if Judge 2 had voted to accept the candidate. If the vote is still split, the candidate is not accepted, but is encouraged to reapply in 3 years.

    The output format says that the program should echo-print the input then output the judges votes and the final decision. In the case of a split decision, both sets of votes should be printed out. If the candidate is too young or has failed the exams, a message of ineligibility is printed (no vote is taken). If an invalid value is read in (ex: age = 200 or income = $2000), the program should print out an error message and go on to the next applicant.

    My teacher mentioned something about using notepad or excel to input data in compliance with an <fstream> header, only problem is I don't know how. Plus, when I put in my code it just says "Input Candidate's ID #"

    [IMG][/IMG]

    Here's my code:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main (){
    	int num, age, social, art, income;
    	int dec;
    	int tievote;
    	int i;
    	
    	char parents;
    	ofstream results;
    	results.open("/Users/putyoursoxon/Documents/results.txt");//application results go here
    		
    	for(i = 1; i <= 3; i ++)
    	{
    		dec = 0;
    		cout << "Input canidate's ID #" << endl;
    		cin >> num;
    		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
    		if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || parents != 'y' && parents != 'n')
    			results << "There is a error in the data" << endl;
    			else if ( age < 0 || age > 120)
    			results << "The age of canidate ID#" << num << " is invalid" << endl; 
    		
    			else if (social < 0 || social > 100)
    			results << "The social skills test score of canidate ID#" << num << " is invaid" << endl;
    		
    		    else if ( art < 0 || art > 100)
    			results << "The art and history test score of canidate ID#" << num << " is invalid" << endl;
    		
    		    else if (income < 5000 || income > 9999999)
    			results << "The yearly income of canidate ID#" << num << " is invalid" << endl;
    		
    		    else if (parents != 'y' && parents != 'n')
    			results << "The input for status of canidate ID#" << num << " parents is not a valid input" << endl;
    		    //Checks that the input data is within the range required
    		    
    			else if ( age < 30)
    			results << "Canidate ID#" << num << " is not old enough to apply. Please reapply in " << 30 - age << " years" << endl;
    		
    		    else if ( social < 60)
    			results << "Canidate ID#" << num << " has failed the social skills test and cannot apply" << endl;
    		
    		    else if ( art < 60)
    			results << "Canidate ID#" << num << " has failed the art and history test and cannot apply" << endl;
    		
    		else
    			if ( ((social + art) / 2) >= 90)
    			dec ++;
    			if ( (income / 1000) > (age * 2))
    			dec ++;
    			if (parents = 'y' && social >= 85)
    			dec ++;
    			if (age < 35 && income > 200000 || parents != 'y' || social < 85)
    			dec ++;
    			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
    				results <<"Its a tie!"<< endl;
    		}
    				
    	
    	results.close();
    	return 0;
    }
    Can You guys please help?
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    I'm taking a look at it; one thing you're doing wrong is here:
    Code:
    if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || parents != 'y' && parents != 'n') 
                results << "There is a error in the data" << endl; 
                else if ( age < 0 || age > 120) 
                results << "The age of canidate ID#" << num << " is invalid" << endl;  
      
                else if (social < 0 || social > 100) 
                results << "The social skills test score of canidate ID#" << num << " is invaid" << endl; 
      
                else if ( art < 0 || art > 100) 
                results << "The art and history test score of canidate ID#" << num << " is invalid" << endl; 
      
                else if (income < 5000 || income > 9999999) 
                results << "The yearly income of canidate ID#" << num << " is invalid" << endl; 
      
                else if (parents != 'y' && parents != 'n') 
                results << "The input for status of canidate ID#" << num << " parents is not a valid input" << endl; 
                //Checks that the input data is within the range required 
      
                else if ( age < 30) 
                results << "Canidate ID#" << num << " is not old enough to apply. Please reapply in " << 30 - age << " years" << endl; 
      
                else if ( social < 60) 
                results << "Canidate ID#" << num << " has failed the social skills test and cannot apply" << endl; 
      
                else if ( art < 60) 
                results << "Canidate ID#" << num << " has failed the art and history test and cannot apply" << endl;
    These else-ifs are not within the larger error if-statement, and even if they were, they should not be else-ifs. Having a series of else-ifs ensures that only one of them gets selected, but there might be multiple problems. Make them all ifs, and put them all in curly braces to put them inside of the large if-statement. Also, put the two parents expressions in parentheses to ensure the order of evaluation is correct. So:
    Code:
    if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || (parents != 'y' && parents != 'n')) {
        results << "There is a error in the data" << endl;
        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;
        // And so on.
    }
    Originally posted by HypeBeast McStreetwear
    Plus, when I put in my code it just says "Input Candidate's ID #"
    Your program is reading from the console, not a file. Try actually entering the candidate's ID #.

    Hope this helps.

    Comment

    • boxfish
      Recognized Expert Contributor
      • Mar 2008
      • 469

      #3
      To get input from a file using an fstream, you open a file with an ifstream just like you do with an ofstream, and read data in from it just like you would from cin. Close it when you're done.

      Edit:
      I guess I should have generalized in my first post: Any loop, if, else, or else-if has to have curly braces around all the statements in it if it has more than one statement in it. The statements in curly braces are called a compound statement. There is at least one other place in your program where you need to do this.

      Comment

      • HypeBeast McStreetwear
        New Member
        • Sep 2008
        • 20

        #4
        Thanks but I tried inputting a file but I don't know how.

        Is the output going to be on the Notepad file "results.da t" or in the black output box?

        Plus I keep getting this error:

        ------ Build started: Project: JudgeLab2, Configuration: Debug Win32 ------
        Compiling...
        JudgeLab2.cpp
        c:\documents and settings\half' amazin\my documents\visua l studio 2008\projects\j udgelab2\judgel ab2\judgelab2.c pp(14) : warning C4129: 'D' : unrecognized character escape sequence
        c:\documents and settings\half' amazin\my documents\visua l studio 2008\projects\j udgelab2\judgel ab2\judgelab2.c pp(14) : warning C4129: 'A' : unrecognized character escape sequence
        c:\documents and settings\half' amazin\my documents\visua l studio 2008\projects\j udgelab2\judgel ab2\judgelab2.c pp(14) : warning C4129: 'D' : unrecognized character escape sequence
        c:\documents and settings\half' amazin\my documents\visua l studio 2008\projects\j udgelab2\judgel ab2\judgelab2.c pp(14) : warning C4129: 'M' : unrecognized character escape sequence
        c:\documents and settings\half' amazin\my documents\visua l studio 2008\projects\j udgelab2\judgel ab2\judgelab2.c pp(14) : error C2660: 'std::basic_str ing<_Elem,_Trai ts,_Ax>::c_str' : function does not take 1 arguments
        with
        [
        _Elem=char,
        _Traits=std::ch ar_traits<char> ,
        _Ax=std::alloca tor<char>
        ]
        Build log was saved at "file://c:\Documents and Settings\Half' Amazin\My Documents\Visua l Studio 2008\Projects\J udgeLab2\JudgeL ab2\Debug\Build Log.htm"
        JudgeLab2 - 1 error(s), 4 warning(s)
        ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          Your program didn't give me any errors when I compiled it. Did you modify the program, and that caused errors?

          Originally posted by HypeBeast McStreetwear
          Is the output going to be on the Notepad file "results.da t" or in the black output box?
          The output is going to be in /Users/putyoursoxon/Documents/results.txt. I do get output when I run your program.

          Comment

          • HypeBeast McStreetwear
            New Member
            • Sep 2008
            • 20

            #6
            Yea, I modified it. Can you repost the program you have?

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              Sure.

              Code:
              #include <iostream>
              #include <fstream>
              
              using namespace std;
              
              int main (){
                  int num, age, social, art, income;
                  int dec;
                  int tievote;
                  int i;
              
                  char parents;
                  ofstream results;
                  results.open("/Users/putyoursoxon/Documents/results.txt");//application results go here
              
                  for(i = 1; i <= 3; i ++)
                  {
                      dec = 0;
                      cout << "Input canidate's ID #" << endl;
                      cin >> num;
                      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
                      if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || (parents != 'y' && parents != 'n')){
                          results << "There is a error in the data" << endl;
                          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;
                          //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;
                      } else {
                          if ( ((social + art) / 2) >= 90)
                              dec ++;
                          if ( (income / 1000) > (age * 2))
                              dec ++;
                          if (parents = 'y' && social >= 85)
                              dec ++;
                          if (age < 35 && income > 200000 || parents != 'y' || social < 85)
                              dec ++;
                          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
                              results <<"Its a tie!"<< endl;
                          }
                      }
                  }
              
              
                  results.close();
                  return 0;
              }

              Comment

              • HypeBeast McStreetwear
                New Member
                • Sep 2008
                • 20

                #8
                See now I get this output

                Code:
                ------ Build started: Project: JudgeLab3, Configuration: Debug Win32 ------
                Compiling...
                JudgeLab3.cpp
                c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(10) : warning C4101: 'tievote' : unreferenced local variable
                c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(82) : error C2143: syntax error : missing ';' before '.'
                c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(82) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
                c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(83) : error C2059: syntax error : 'return'
                c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(84) : error C2059: syntax error : '}'
                c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(84) : error C2143: syntax error : missing ';' before '}'
                c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(84) : error C2059: syntax error : '}'
                Build log was saved at "file://c:\Documents and Settings\Half' Amazin\My Documents\Visual Studio 2008\Projects\JudgeLab3\JudgeLab3\Debug\BuildLog.htm"
                JudgeLab3 - 6 error(s), 1 warning(s)
                ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  Oh sorry. I added too many closing braces here:
                  Code:
                              else
                                  results <<"Its a tie!"<< endl;
                              }
                          }
                      }
                  
                  
                      results.close();
                  Change it to:

                  Code:
                              else
                                  results <<"Its a tie!"<< endl;
                          }
                      }
                  
                  
                      results.close();

                  Comment

                  • HypeBeast McStreetwear
                    New Member
                    • Sep 2008
                    • 20

                    #10
                    Oh lol, thanks tho. I'm so close >=(

                    I get this now error now:

                    -
                    Code:
                    ----- Build started: Project: JudgeLab3, Configuration: Debug Win32 ------
                    Compiling...
                    JudgeLab3.cpp
                    c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(82) : fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(7)' was matched
                    Build log was saved at "file://c:\Documents and Settings\Half' Amazin\My Documents\Visual Studio 2008\Projects\JudgeLab3\JudgeLab3\Debug\BuildLog.htm"
                    JudgeLab3 - 1 error(s), 0 warning(s)
                    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      Did you put
                      Code:
                          return 0;
                      }
                      back after you swiched the code? Because it's saying that main is missing the closing brace.

                      Comment

                      • HypeBeast McStreetwear
                        New Member
                        • Sep 2008
                        • 20

                        #12
                        Ok it ran but I still get "Input Candidate's ID#"

                        I know I have to put in the Candidate's # but where? And where will the program read the info from? The Notepad file?

                        Comment

                        • boxfish
                          Recognized Expert Contributor
                          • Mar 2008
                          • 469

                          #13
                          The program will not read the info from a file until you get it working using an ifstream. For now, when it asks for the id #, just type it into the console window and press enter. Debug it until it works this way, then get it reading from an ifstream (see my reply #3).

                          Comment

                          • HypeBeast McStreetwear
                            New Member
                            • Sep 2008
                            • 20

                            #14
                            Ok. Im so confused. But yea, I get the prompts in the output box, like I can manually input AGE, SCORES etc. But just dont understand how to use the ifstream :-/

                            Comment

                            • boxfish
                              Recognized Expert Contributor
                              • Mar 2008
                              • 469

                              #15
                              It's a lot like ofstream. ifstream is to cin what ofstream is to cout. Look it up in your textbook or google it.

                              Comment

                              Working...