473,804 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Looping/Inputting data (fstream)

20 New Member
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 2026
boxfish
469 Recognized Expert Contributor
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 Recognized Expert Contributor
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
HypeBeast McStreetwear
20 New Member
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 ==========
Oct 2 '08 #4
boxfish
469 Recognized Expert Contributor
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.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.
Oct 2 '08 #5
HypeBeast McStreetwear
20 New Member
Yea, I modified it. Can you repost the program you have?
Oct 2 '08 #6
boxfish
469 Recognized Expert Contributor
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
HypeBeast McStreetwear
20 New Member
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 Recognized Expert Contributor
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
HypeBeast McStreetwear
20 New Member
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

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

Similar topics

4
8082
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 has data send to server. 2. Client send the buffer block size. 3. Client send the actual buffer to the server. 4. Server send a flag 1 to client indicating that the buffer has been successfully receeived. 5. The next loop until all data of the...
10
2360
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
2388
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 vector. This is what I have till now: #include <iostream> #include "maze.h" #include <vector> #include <fstream>
9
5295
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 since I will need to write and read from files, and I am using those files as binary files. I made a sample of what's going on (below). Without using vector, everything works fine, but with using vectors, something is going wrong somewhere!?? ...
2
7451
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 'begin measurement' and 'end measurement' markers with different text within each one. /begin MEASUREMENT E2_PCU_90V "The 90v rail PWM trim value" UWORD ...
1
2552
by: Ramper | last post by:
Have a .txt document as: String int int int int int int int
2
1770
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) contains r on the left hand side and theta on the right side 5 0.5235 7 0.5345 4 0.5642 int main() { fstream fo;
6
3266
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
1409
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 output a statement of acceptance for each applicant. This is program I am using: #include <iostream> #include <fstream> using namespace std;
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.