Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. file input program

file input program

Scheduled Pinned Locked Moved C / C++ / MFC
helpios
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    klutez123
    wrote on last edited by
    #1

    I'm trying to make a program to open a file named stats.dat and input name, batting average, and home run amount. So far I have: #include "stdafx.h" #include "statistics.h" #include #include #include // Global Declarations using namespace std; CWinApp TheApp; /***************************************************** * MAIN * *****************************************************/ int main() { //variable declarations char end, lc; //declare char int hr_num; //declare int float bat_avg; //declare float string name; //declare string ofstream outfile; //declare an ofstream filestream outfile.open("FLOATS.DAT",ios::app); //open file for output lc = 'y'; if(outfile) //if file opened successfully { while (lc = 'y') // { cout << "Enter the name of the player: "; getline(cin, name); outfile << name<> bat_avg; outfile << bat_avg<> hr_num; outfile << hr_num<> lc; } //while x does not equal 0 } else //if file failed to open... { cout << "Error opening file.\n"; //output error message } outfile.close(); //close file return 0; //terminate program } My problem is that after the .exe runs through once and will skip lines the second time through. All help is appreciated

    D T J 3 Replies Last reply
    0
    • K klutez123

      I'm trying to make a program to open a file named stats.dat and input name, batting average, and home run amount. So far I have: #include "stdafx.h" #include "statistics.h" #include #include #include // Global Declarations using namespace std; CWinApp TheApp; /***************************************************** * MAIN * *****************************************************/ int main() { //variable declarations char end, lc; //declare char int hr_num; //declare int float bat_avg; //declare float string name; //declare string ofstream outfile; //declare an ofstream filestream outfile.open("FLOATS.DAT",ios::app); //open file for output lc = 'y'; if(outfile) //if file opened successfully { while (lc = 'y') // { cout << "Enter the name of the player: "; getline(cin, name); outfile << name<> bat_avg; outfile << bat_avg<> hr_num; outfile << hr_num<> lc; } //while x does not equal 0 } else //if file failed to open... { cout << "Error opening file.\n"; //output error message } outfile.close(); //close file return 0; //terminate program } My problem is that after the .exe runs through once and will skip lines the second time through. All help is appreciated

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      klutez123 wrote:

      while (lc = 'y') //

      If you'll put constants on the left side of the operator, you won't have such problems. Having while ('y' = lc), the compiler would have immediately told you the problem.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      1 Reply Last reply
      0
      • K klutez123

        I'm trying to make a program to open a file named stats.dat and input name, batting average, and home run amount. So far I have: #include "stdafx.h" #include "statistics.h" #include #include #include // Global Declarations using namespace std; CWinApp TheApp; /***************************************************** * MAIN * *****************************************************/ int main() { //variable declarations char end, lc; //declare char int hr_num; //declare int float bat_avg; //declare float string name; //declare string ofstream outfile; //declare an ofstream filestream outfile.open("FLOATS.DAT",ios::app); //open file for output lc = 'y'; if(outfile) //if file opened successfully { while (lc = 'y') // { cout << "Enter the name of the player: "; getline(cin, name); outfile << name<> bat_avg; outfile << bat_avg<> hr_num; outfile << hr_num<> lc; } //while x does not equal 0 } else //if file failed to open... { cout << "Error opening file.\n"; //output error message } outfile.close(); //close file return 0; //terminate program } My problem is that after the .exe runs through once and will skip lines the second time through. All help is appreciated

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        klutez123 wrote:

        while (lc = 'y')

        my answer is certainly not related to your question, but are you sure you didn't want to write this instead ?

        while (lc **==** 'y')


        [VisualCalc][Flags Beginner's Guide] | [Forums Guidelines][My Best Advice]

        1 Reply Last reply
        0
        • K klutez123

          I'm trying to make a program to open a file named stats.dat and input name, batting average, and home run amount. So far I have: #include "stdafx.h" #include "statistics.h" #include #include #include // Global Declarations using namespace std; CWinApp TheApp; /***************************************************** * MAIN * *****************************************************/ int main() { //variable declarations char end, lc; //declare char int hr_num; //declare int float bat_avg; //declare float string name; //declare string ofstream outfile; //declare an ofstream filestream outfile.open("FLOATS.DAT",ios::app); //open file for output lc = 'y'; if(outfile) //if file opened successfully { while (lc = 'y') // { cout << "Enter the name of the player: "; getline(cin, name); outfile << name<> bat_avg; outfile << bat_avg<> hr_num; outfile << hr_num<> lc; } //while x does not equal 0 } else //if file failed to open... { cout << "Error opening file.\n"; //output error message } outfile.close(); //close file return 0; //terminate program } My problem is that after the .exe runs through once and will skip lines the second time through. All help is appreciated

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          Totally unrealated to your question, but this

          char end, lc; //declare char

          is an example for a very bad comment. Everyone who knows a little C++ knows the he read a char variable declaration before he even comes to reading the comment. Write what the role of the variables will be, comment why, not what.


          Failure is not an option - it's built right in.

          S 1 Reply Last reply
          0
          • J jhwurmbach

            Totally unrealated to your question, but this

            char end, lc; //declare char

            is an example for a very bad comment. Everyone who knows a little C++ knows the he read a char variable declaration before he even comes to reading the comment. Write what the role of the variables will be, comment why, not what.


            Failure is not an option - it's built right in.

            S Offline
            S Offline
            Sebastian Schneider
            wrote on last edited by
            #5

            I would even go as far as saying that the comment is plain wrong. He is not declaring char, he is declaring the variables end and lc.

            Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups