473,327 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main (){
  7.     int num, age, social, art, income;
  8.     int dec;
  9.     int tievote;
  10.     int i;
  11.  
  12.     char parents;
  13.     ofstream results;
  14.     results.open("/Users/putyoursoxon/Documents/results.txt");//application results go here
  15.  
  16.     for(i = 1; i <= 3; i ++)
  17.     {
  18.         dec = 0;
  19.         cout << "Input canidate's ID #" << endl;
  20.         cin >> num;
  21.         cout << "Input canidate's age" << endl;
  22.         cin >> age;
  23.         cout << "Input canidate's social skills test score" << endl;
  24.         cin >> social;
  25.         cout << "Input canidate's art and history test score" << endl;
  26.         cin >> art;
  27.         cout << "Input canidate's income" << endl;
  28.         cin >> income;
  29.         cout <<"Are canidate's parents in the WTS?" << endl;
  30.         cin >> parents;
  31.         results << "Canidate ID#" << num << ", Age =" << age << ", Social skils test score =" << social << endl;
  32.         results << "Art and history test score =" << art << ", Income =$" << income << ", Parents in WTS? =" << parents << endl;
  33.         results << endl;
  34.         //display input data
  35.         if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || parents != 'y' && parents != 'n')
  36.             results << "There is a error in the data" << endl;
  37.             else if ( age < 0 || age > 120)
  38.             results << "The age of canidate ID#" << num << " is invalid" << endl; 
  39.  
  40.             else if (social < 0 || social > 100)
  41.             results << "The social skills test score of canidate ID#" << num << " is invaid" << endl;
  42.  
  43.             else if ( art < 0 || art > 100)
  44.             results << "The art and history test score of canidate ID#" << num << " is invalid" << endl;
  45.  
  46.             else if (income < 5000 || income > 9999999)
  47.             results << "The yearly income of canidate ID#" << num << " is invalid" << endl;
  48.  
  49.             else if (parents != 'y' && parents != 'n')
  50.             results << "The input for status of canidate ID#" << num << " parents is not a valid input" << endl;
  51.             //Checks that the input data is within the range required
  52.  
  53.             else if ( age < 30)
  54.             results << "Canidate ID#" << num << " is not old enough to apply. Please reapply in " << 30 - age << " years" << endl;
  55.  
  56.             else if ( social < 60)
  57.             results << "Canidate ID#" << num << " has failed the social skills test and cannot apply" << endl;
  58.  
  59.             else if ( art < 60)
  60.             results << "Canidate ID#" << num << " has failed the art and history test and cannot apply" << endl;
  61.  
  62.         else
  63.             if ( ((social + art) / 2) >= 90)
  64.             dec ++;
  65.             if ( (income / 1000) > (age * 2))
  66.             dec ++;
  67.             if (parents = 'y' && social >= 85)
  68.             dec ++;
  69.             if (age < 35 && income > 200000 || parents != 'y' || social < 85)
  70.             dec ++;
  71.             if (dec>2)
  72.                 results << "Canidate ID#" << num << " has been accepted into the Windsor Tea Society with " << dec << " votes. Congratulations!" << endl;
  73.             else if (dec<2)
  74.                 results << "Canidate ID#" << num << " has not been accepted into the Winsor Tea Society with only " << dec << " votes. Sorry :-(" << endl;
  75.             else
  76.                 results <<"Its a tie!"<< endl;
  77.         }
  78.  
  79.  
  80.     results.close();
  81.     return 0;
  82. }
Can You guys please help?
Oct 1 '08 #1
16 1987
boxfish
469 Expert 256MB
I'm taking a look at it; one thing you're doing wrong is here:
Expand|Select|Wrap|Line Numbers
  1. if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || parents != 'y' && parents != 'n') 
  2.             results << "There is a error in the data" << endl; 
  3.             else if ( age < 0 || age > 120) 
  4.             results << "The age of canidate ID#" << num << " is invalid" << endl;  
  5.  
  6.             else if (social < 0 || social > 100) 
  7.             results << "The social skills test score of canidate ID#" << num << " is invaid" << endl; 
  8.  
  9.             else if ( art < 0 || art > 100) 
  10.             results << "The art and history test score of canidate ID#" << num << " is invalid" << endl; 
  11.  
  12.             else if (income < 5000 || income > 9999999) 
  13.             results << "The yearly income of canidate ID#" << num << " is invalid" << endl; 
  14.  
  15.             else if (parents != 'y' && parents != 'n') 
  16.             results << "The input for status of canidate ID#" << num << " parents is not a valid input" << endl; 
  17.             //Checks that the input data is within the range required 
  18.  
  19.             else if ( age < 30) 
  20.             results << "Canidate ID#" << num << " is not old enough to apply. Please reapply in " << 30 - age << " years" << endl; 
  21.  
  22.             else if ( social < 60) 
  23.             results << "Canidate ID#" << num << " has failed the social skills test and cannot apply" << endl; 
  24.  
  25.             else if ( art < 60) 
  26.             results << "Canidate ID#" << num << " has failed the art and history test and cannot apply" << endl; 
  27.  
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:
Expand|Select|Wrap|Line Numbers
  1. if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || (parents != 'y' && parents != 'n')) {
  2.     results << "There is a error in the data" << endl;
  3.     if ( age < 0 || age > 120)
  4.         results << "The age of canidate ID#" << num << " is invalid" << endl;
  5.  
  6.     if (social < 0 || social > 100)
  7.         results << "The social skills test score of canidate ID#" << num << " is invaid" << endl;
  8.     // And so on.
  9. }
  10.  
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.
Oct 2 '08 #2
boxfish
469 Expert 256MB
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.
Oct 2 '08 #3
Thanks but I tried inputting a file but I don't know how.

Is the output going to be on the Notepad file "results.dat" 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\visual studio 2008\projects\judgelab2\judgelab2\judgelab2.cpp(14 ) : warning C4129: 'D' : unrecognized character escape sequence
c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab2\judgelab2\judgelab2.cpp(14 ) : warning C4129: 'A' : unrecognized character escape sequence
c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab2\judgelab2\judgelab2.cpp(14 ) : warning C4129: 'D' : unrecognized character escape sequence
c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab2\judgelab2\judgelab2.cpp(14 ) : warning C4129: 'M' : unrecognized character escape sequence
c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab2\judgelab2\judgelab2.cpp(14 ) : error C2660: 'std::basic_string<_Elem,_Traits,_Ax>::c_str' : function does not take 1 arguments
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
Build log was saved at "file://c:\Documents and Settings\Half' Amazin\My Documents\Visual Studio 2008\Projects\JudgeLab2\JudgeLab2\Debug\BuildLog.h tm"
JudgeLab2 - 1 error(s), 4 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Oct 2 '08 #4
boxfish
469 Expert 256MB
Your program didn't give me any errors when I compiled it. Did you modify the program, and that caused errors?

Is the output going to be on the Notepad file "results.dat" 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.
Oct 2 '08 #5
Yea, I modified it. Can you repost the program you have?
Oct 2 '08 #6
boxfish
469 Expert 256MB
Sure.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main (){
  7.     int num, age, social, art, income;
  8.     int dec;
  9.     int tievote;
  10.     int i;
  11.  
  12.     char parents;
  13.     ofstream results;
  14.     results.open("/Users/putyoursoxon/Documents/results.txt");//application results go here
  15.  
  16.     for(i = 1; i <= 3; i ++)
  17.     {
  18.         dec = 0;
  19.         cout << "Input canidate's ID #" << endl;
  20.         cin >> num;
  21.         cout << "Input canidate's age" << endl;
  22.         cin >> age;
  23.         cout << "Input canidate's social skills test score" << endl;
  24.         cin >> social;
  25.         cout << "Input canidate's art and history test score" << endl;
  26.         cin >> art;
  27.         cout << "Input canidate's income" << endl;
  28.         cin >> income;
  29.         cout <<"Are canidate's parents in the WTS?" << endl;
  30.         cin >> parents;
  31.         results << "Canidate ID#" << num << ", Age =" << age << ", Social skils test score =" << social << endl;
  32.         results << "Art and history test score =" << art << ", Income =$" << income << ", Parents in WTS? =" << parents << endl;
  33.         results << endl;
  34.         //display input data
  35.         if ( age < 30 || age > 120 || social < 60 || social > 100 || art < 60 || art > 100 || income < 5000 || income > 9999999 || (parents != 'y' && parents != 'n')){
  36.             results << "There is a error in the data" << endl;
  37.             if ( age < 0 || age > 120)
  38.                 results << "The age of canidate ID#" << num << " is invalid" << endl;
  39.  
  40.             if (social < 0 || social > 100)
  41.                 results << "The social skills test score of canidate ID#" << num << " is invaid" << endl;
  42.  
  43.             if ( art < 0 || art > 100)
  44.                 results << "The art and history test score of canidate ID#" << num << " is invalid" << endl;
  45.  
  46.             if (income < 5000 || income > 9999999)
  47.                 results << "The yearly income of canidate ID#" << num << " is invalid" << endl;
  48.  
  49.             if (parents != 'y' && parents != 'n')
  50.                 results << "The input for status of canidate ID#" << num << " parents is not a valid input" << endl;
  51.             //Checks that the input data is within the range required
  52.  
  53.             if ( age < 30)
  54.                 results << "Canidate ID#" << num << " is not old enough to apply. Please reapply in " << 30 - age << " years" << endl;
  55.  
  56.             if ( social < 60)
  57.                 results << "Canidate ID#" << num << " has failed the social skills test and cannot apply" << endl;
  58.  
  59.             if ( art < 60)
  60.             results << "Canidate ID#" << num << " has failed the art and history test and cannot apply" << endl;
  61.         } else {
  62.             if ( ((social + art) / 2) >= 90)
  63.                 dec ++;
  64.             if ( (income / 1000) > (age * 2))
  65.                 dec ++;
  66.             if (parents = 'y' && social >= 85)
  67.                 dec ++;
  68.             if (age < 35 && income > 200000 || parents != 'y' || social < 85)
  69.                 dec ++;
  70.             if (dec>2)
  71.                 results << "Canidate ID#" << num << " has been accepted into the Windsor Tea Society with " << dec << " votes. Congratulations!" << endl;
  72.             else if (dec<2)
  73.                 results << "Canidate ID#" << num << " has not been accepted into the Winsor Tea Society with only " << dec << " votes. Sorry :-(" << endl;
  74.             else
  75.                 results <<"Its a tie!"<< endl;
  76.             }
  77.         }
  78.     }
  79.  
  80.  
  81.     results.close();
  82.     return 0;
  83. }
  84.  
Oct 2 '08 #7
See now I get this output

Expand|Select|Wrap|Line Numbers
  1. ------ Build started: Project: JudgeLab3, Configuration: Debug Win32 ------
  2. Compiling...
  3. JudgeLab3.cpp
  4. c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(10) : warning C4101: 'tievote' : unreferenced local variable
  5. c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(82) : error C2143: syntax error : missing ';' before '.'
  6. 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
  7. c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(83) : error C2059: syntax error : 'return'
  8. c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(84) : error C2059: syntax error : '}'
  9. c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(84) : error C2143: syntax error : missing ';' before '}'
  10. c:\documents and settings\half' amazin\my documents\visual studio 2008\projects\judgelab3\judgelab3\judgelab3.cpp(84) : error C2059: syntax error : '}'
  11. Build log was saved at "file://c:\Documents and Settings\Half' Amazin\My Documents\Visual Studio 2008\Projects\JudgeLab3\JudgeLab3\Debug\BuildLog.htm"
  12. JudgeLab3 - 6 error(s), 1 warning(s)
  13. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  14.  
Oct 2 '08 #8
boxfish
469 Expert 256MB
Oh sorry. I added too many closing braces here:
Expand|Select|Wrap|Line Numbers
  1.             else
  2.                 results <<"Its a tie!"<< endl;
  3.             }
  4.         }
  5.     }
  6.  
  7.  
  8.     results.close();
  9.  
Change it to:

Expand|Select|Wrap|Line Numbers
  1.             else
  2.                 results <<"Its a tie!"<< endl;
  3.         }
  4.     }
  5.  
  6.  
  7.     results.close();
  8.  
Oct 2 '08 #9
Oh lol, thanks tho. I'm so close >=(

I get this now error now:

-
Expand|Select|Wrap|Line Numbers
  1. ----- Build started: Project: JudgeLab3, Configuration: Debug Win32 ------
  2. Compiling...
  3. JudgeLab3.cpp
  4. 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
  5. Build log was saved at "file://c:\Documents and Settings\Half' Amazin\My Documents\Visual Studio 2008\Projects\JudgeLab3\JudgeLab3\Debug\BuildLog.htm"
  6. JudgeLab3 - 1 error(s), 0 warning(s)
  7. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  8.  
Oct 2 '08 #10
boxfish
469 Expert 256MB
Did you put
Expand|Select|Wrap|Line Numbers
  1.     return 0;
  2. }
  3.  
back after you swiched the code? Because it's saying that main is missing the closing brace.
Oct 2 '08 #11
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?
Oct 2 '08 #12
boxfish
469 Expert 256MB
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).
Oct 2 '08 #13
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 :-/
Oct 2 '08 #14
boxfish
469 Expert 256MB
It's a lot like ofstream. ifstream is to cin what ofstream is to cout. Look it up in your textbook or google it.
Oct 2 '08 #15
Yea, Im reading my textbook. Its just a matter of what to write and where to put it in this program :-/
Oct 2 '08 #16
Thanks alot for your help but I think Im just gonna settle with what I have.
Oct 2 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: zhimin | last post by:
Hi, I'm writing a program to send large file(100m) through dotnet using TCPListener & TCPClient, I'm sending the file with a ask and response loop: 1. Client send a flag 1 to server indicate it...
10
by: kathy | last post by:
to read float type data, we can use: .... float f; fscanf(outfile, "%f", &f); .... but for double type, how to do that? .... double d;
2
by: saltedcoffee | last post by:
hey, I am beginer to C++ programing. I am working on a project called the "maze Problem" First of all I am required to read a text file in( which is the maze) and store it into a 2 dimentional...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
2
by: Lana rose | last post by:
I am trying to produce some code in C++ that will be able to scan through a mixed document and extract specific lines of data. The document will look like this (below) but will have hundreds of these...
1
by: Ramper | last post by:
Have a .txt document as: String int int int int int int int
2
by: norm9875 | last post by:
Hi...everybody...I'm stuck in fstream...can anyone please be kind helping me out of my problem. I don't know how to use my data file correctly ........For example now I have data file (data.data)...
6
namcintosh
by: namcintosh | last post by:
First of all, here is my program: #include <iostream> #include <conio> #include <fstream> //Needed to use files #include <iostream> #include <fstream>
2
by: HypeBeast McStreetwear | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.