Welcome, Register for free! or Login below:
EduGeek.net RSS Feeds Register FAQ Members Social Groups User Map Calendar Search Today's Posts Mark Forums Read

Go Back   EduGeek.net Forums > Coding and Web Development > Coding
Reply
 
LinkBack Thread Tools Search Thread Language
Sponsored Links
Old 19-07-2008, 10:51 PM   #1
 
greguk's Avatar
 
Join Date: Apr 2008
Posts: 4
uk
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0 greguk is an unknown quantity at this point
Unhappy help with simple code

Hi,

I'm trying to revise my "knowledge" in C++ and program probably worked is doing something stupid. Program should read from file First name then last name and finally mark that person got, then move to another person and so on. Next step program asks user to enter his score and compare this score with scores which read from file (Mark < Score) and display and write to file everyone who got higher mark than user.

Instead of that program writes to file details of first person from file either she got higher score than user or not (doesn't follow program condition).
Here is a code:

// lab5_q1.cpp : Defines the entry point for the console application.
// fstream.cpp
//

#include <stdafx.h>
#include <fstream>
#include <iostream>
using namespace std;


void main()
{
ifstream InFile;
ofstream OutFile;
char FirstName[20], LastName[20];
int Score;
int Mark;

cout << "Please enter your mark: ";
cin >> Mark;

InFile.open("Records.txt");
InFile >> FirstName >> LastName >> Score;
OutFile.open("HighScores.txt");
OutFile << FirstName << LastName << Score;


while (!InFile.eof())
{

InFile >> FirstName >> LastName >> Score;

if (Mark < Score)
{
cout << FirstName << " " << LastName << " " << Score << endl;
OutFile << FirstName << " " << LastName << " " << Score << endl;
}

}
InFile.close();
OutFile.close();


system("pause");
}

Note: if you want to test this code just create file records.txt with example names i.e

John Doe 65
Jane Smith 42
Carl Young 59
Sarah Jones 77
Kevin Harris 68
Chris Billington 73

I bet it's simple but i'm groping.

Thanks
  Reply With Quote
Old 20-07-2008, 01:33 AM   #2
 
dhicks's Avatar
 
Join Date: Aug 2005
Location: Alton, Hampshire
Posts: 1,376
Thanks: 119
Thanked 91 Times in 86 Posts
Rep Power: 28 dhicks is a name known to alldhicks is a name known to alldhicks is a name known to alldhicks is a name known to alldhicks is a name known to alldhicks is a name known to all
Default

Quote:
Originally Posted by greguk View Post
I'm trying to revise my "knowledge" in C++
It's been a while since I did any C++, I should do this too...

Quote:
Instead of that program writes to file details of first person from file either she got higher score than user or not
That's because that's exactly what you tell it to do:

Quote:
Code:
InFile.open("Records.txt");
InFile >> FirstName >> LastName >> Score;
OutFile.open("HighScores.txt");
OutFile << FirstName << LastName << Score;
Try just:

Code:
InFile.open("Records.txt");
OutFile.open("HighScores.txt");
Quote:
// lab5_q1.cpp
Lab 5 question 1? Are you sure this isn't homework?

Quote:
#include <stdafx.h>
Note that this is Windows-specific.

Quote:
I bet it's simple but i'm groping.
Maybe that's your problem - try keeping both hands on the keyboard next time, aids concentration.

--
David Hicks
  Reply With Quote
Old 20-07-2008, 05:40 PM   #3
 
greguk's Avatar
 
Join Date: Apr 2008
Posts: 4
uk
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0 greguk is an unknown quantity at this point
Smile

Works.. Thank You David.. Seems like i didn't pay attention somewhere

Greg
  Reply With Quote
Reply

Register now for FREE and post messages!


Username: Password: Confirm Password: E-Mail: Confirm E-Mail:
Birthday:      
Image Verification
  I agree to forum rules 

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple 1.5 doesn't look right SimpleSi EduGeek Joomla 1.5 Package 7 25-05-2008 11:21 AM
Simple php framework Jona Web Development 2 02-05-2008 02:43 PM
Simple Gallery Scotmk Web Development 2 05-03-2008 01:14 PM
I love a simple solution laserblazer General Chat 34 12-10-2007 12:26 PM
Cheap and simple NAS adamyoung Hardware 3 21-12-2006 09:52 AM


Tags
c++, code, programming


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search Thread
Search Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +1. The time now is 08:56 AM.
Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
Copyright EduGeek.net