+ Post New Thread
Results 1 to 3 of 3
Coding Thread, help with simple code in Coding and Web Development; Hi, I'm trying to revise my "knowledge" in C++ and program probably worked is doing something stupid. Program should read ...
  1. #1

    Join Date
    Apr 2008
    Posts
    4
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    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

  2. #2

    dhicks's Avatar
    Join Date
    Aug 2005
    Location
    Knightsbridge
    Posts
    5,723
    Thank Post
    1,286
    Thanked 797 Times in 693 Posts
    Rep Power
    239
    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...

    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:

    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");
    // lab5_q1.cpp
    Lab 5 question 1? Are you sure this isn't homework?

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

    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

  3. #3

    Join Date
    Apr 2008
    Posts
    4
    Thank Post
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Smile

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

    Greg



SHARE:
+ Post New Thread

Similar Threads

  1. Simple 1.5 doesn't look right
    By SimpleSi in forum EduGeek Joomla 1.5 Package
    Replies: 7
    Last Post: 25th May 2008, 12:21 PM
  2. Simple php framework
    By Jona in forum Web Development
    Replies: 2
    Last Post: 2nd May 2008, 03:43 PM
  3. Simple Gallery
    By Scotmk in forum Web Development
    Replies: 2
    Last Post: 5th March 2008, 01:14 PM
  4. I love a simple solution
    By laserblazer in forum General Chat
    Replies: 34
    Last Post: 12th October 2007, 01:26 PM
  5. Cheap and simple NAS
    By adamyoung in forum Hardware
    Replies: 3
    Last Post: 21st December 2006, 09:52 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •