• 0

C++ help


Question

this program needs to save a company's two payroll codes and correspongind salaries in a sequential access file.....

and this is what I have so far:

the only problem is it does not stop when I enter the 3rd salary

Code Salary

A 27200

B 15000

C 23000

#include

#include

#include

using namespace std;

int main()

{

ofstream outFile;

outFile.open("T13ConE09.dats::app);

if (outFile.is_open())

{

string code = "";

int sales = 0;

int counter = 0;

cout << "Code (X to stop): ";

getline(cin, code);

while (counter < 3 )

{

cout << "sales: ";

cin >> sales;

cin.ignore();

counter = counter + 1;

outFile << code << '#' << sales << endl;

cout << "Code: ";

getline(cin, code);

}

outFile.close();

}

else

cout << "File could not be opened. " << endl;

return 0;

}

Link to comment
https://www.neowin.net/forum/topic/20870-c-help/
Share on other sites

13 answers to this question

Recommended Posts

  • 0

Well, I don't know a lick of c++, but i managed to get it to work like this:

&lt;pre&gt;#include &lt;iostream&gt;

#include &lt;fstream&gt;

#include &lt;string&gt;

using namespace std;



int main()

{





  ofstream outFile;

  outFile.open("T13ConE09.dats::app);



  if (outFile.is_open())

    {

      string code = "";

      int sales = 0;

      int counter = 0;

      cout &lt;&lt; "Code (X to stop): ";

      getline(cin, code);

      while (counter &lt; 3 )

	{

   if(counter)

     {

       cout &lt;&lt; "Code: ";

       getline(cin, code);

     }



   cout &lt;&lt; "sales: ";

   cin &gt;&gt; sales;

   cin.ignore();

   counter = counter + 1;

   outFile &lt;&lt; code &lt;&lt; '#' &lt;&lt; sales &lt;&lt; endl;

	}

      outFile.close();

    }

  else

    cout &lt;&lt; "File could not be opened. " &lt;&lt; endl;

  return 0;



}

&lt;/pre&gt;

-Hatter

EDIT: File didn't save properly, fixed.

Link to comment
https://www.neowin.net/forum/topic/20870-c-help/#findComment-176187
Share on other sites

  • 0
  Quote
Originally posted by Goalie_CA

cin supports *char and string. Why would a standard library not be able to support another???

As I mentioned above, i've never coded C++ before nor read about its libraries. I was unsure as to whether cin was similar to getchar() in C or whether it was just antoher stdin function.

-Hatter

Link to comment
https://www.neowin.net/forum/topic/20870-c-help/#findComment-176400
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.