C++ File I/O skipping line every new iteration, why?
Why is the program skipping the first line of every new deptnum?
I am trying to read from a file that looks like:
1 Suits 0300 100 092
1 Coats 0200 060 065
1 Shirts 1000 012 013
2 Dresses 0400 060 065
2 Coats 0185 184 200
2 Shoes 0600 040 030
3 Jeans 0200 040 035
3 Shoes 0200 030 034
4 Jeans 0300 042 043
The deptnum is the first column.
And when I write to the other file I get:
Blinn Discount Apparel Company
Inventory Evaluation
10/12/2018
Unit Cost Extended
Quantity Cost Market Cost Market Lower Cost
Mens Dept
Suits 300 100.00 92.00 30000.00 27600.00
Coats 200 60.00 65.00 12000.00 13000.00
Shirts 1000 12.00 13.00 12000.00 13000.00
Total $54000.00 $53600.00 $53600.00
Womens Dept
Coats 185 184.00 200.00 34040.00 37000.00
Shoes 600 40.00 30.00 24000.00 18000.00
Total $112040.00 $108600.00 $108600.00
Girls Dept
Shoes 200 30.00 34.00 6000.00 6800.00
Total $118040.00 $115400.00 $115400.00
Boys Dept
Total $118040.00 $115400.00 $115400.00
Total Inventory $393000.00
It skipped Womens Dept -> Dresses, Girls Dept -> Jeans, and Boys Dept -> Jeans.
Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
ifstream inFile;
ofstream outFile;
int x = 1, deptnum, quant,cost,mkt,extcost,extmkt,totalcost = 0,totalmkt = 0,
lowcost,totalInv = 0;
char item [15];
inFile.open("blinn.dat");
outFile.open("blinn.dout");
if (!inFile)
cout <<"ntt Can't open data file: blinn.datn";
else
outFile <<"nt Blinn Discount Apparel Companyn";
outFile <<"t Inventory Evaluationn";
outFile <<"t 10/12/2018n";
outFile <<"ntttttt Unit Costttt Extendedn";
outFile <<"tt Quantity Cost Market Cost Market Lower Cost";
while (x < 5)
if (x == 1)
outFile << "nMens Dept";
else if (x == 2)
outFile << "nWomens Dept";
else if (x == 3)
outFile << "nGirls Dept";
else if (x == 4)
outFile << "nBoys Dept";
else
break;
while (inFile >> deptnum >> item >> quant >> cost >> mkt)
if (deptnum == x)
extcost = quant * cost;
extmkt = quant * mkt;
outFile << left << "n " << setw(7)<< item << " "
<< right << setw(4)<< quant << " "
<< right << setw(4) << cost << ".00 "
<< right << setw(3) << mkt << ".00 "
<< right << setw(5) << extcost<< ".00 "
<< right << setw(5) << extmkt << ".00";
totalcost += extcost;
totalmkt += extmkt;
if (totalcost > totalmkt)
lowcost = totalmkt;
else
lowcost = totalcost;
else
break;
outFile << right << "n Totalttttt $" << totalcost << ".00 $"
<< totalmkt << ".00 $"<< lowcost << ".00";
x += 1;
totalInv += lowcost;
outFile << "nTotal Inventorytttttt $"<< totalInv<< ".00";
inFile.close ();
outFile.close ();
return 0;
What is wrong with my logic?
c++ file-io
add a comment |
Why is the program skipping the first line of every new deptnum?
I am trying to read from a file that looks like:
1 Suits 0300 100 092
1 Coats 0200 060 065
1 Shirts 1000 012 013
2 Dresses 0400 060 065
2 Coats 0185 184 200
2 Shoes 0600 040 030
3 Jeans 0200 040 035
3 Shoes 0200 030 034
4 Jeans 0300 042 043
The deptnum is the first column.
And when I write to the other file I get:
Blinn Discount Apparel Company
Inventory Evaluation
10/12/2018
Unit Cost Extended
Quantity Cost Market Cost Market Lower Cost
Mens Dept
Suits 300 100.00 92.00 30000.00 27600.00
Coats 200 60.00 65.00 12000.00 13000.00
Shirts 1000 12.00 13.00 12000.00 13000.00
Total $54000.00 $53600.00 $53600.00
Womens Dept
Coats 185 184.00 200.00 34040.00 37000.00
Shoes 600 40.00 30.00 24000.00 18000.00
Total $112040.00 $108600.00 $108600.00
Girls Dept
Shoes 200 30.00 34.00 6000.00 6800.00
Total $118040.00 $115400.00 $115400.00
Boys Dept
Total $118040.00 $115400.00 $115400.00
Total Inventory $393000.00
It skipped Womens Dept -> Dresses, Girls Dept -> Jeans, and Boys Dept -> Jeans.
Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
ifstream inFile;
ofstream outFile;
int x = 1, deptnum, quant,cost,mkt,extcost,extmkt,totalcost = 0,totalmkt = 0,
lowcost,totalInv = 0;
char item [15];
inFile.open("blinn.dat");
outFile.open("blinn.dout");
if (!inFile)
cout <<"ntt Can't open data file: blinn.datn";
else
outFile <<"nt Blinn Discount Apparel Companyn";
outFile <<"t Inventory Evaluationn";
outFile <<"t 10/12/2018n";
outFile <<"ntttttt Unit Costttt Extendedn";
outFile <<"tt Quantity Cost Market Cost Market Lower Cost";
while (x < 5)
if (x == 1)
outFile << "nMens Dept";
else if (x == 2)
outFile << "nWomens Dept";
else if (x == 3)
outFile << "nGirls Dept";
else if (x == 4)
outFile << "nBoys Dept";
else
break;
while (inFile >> deptnum >> item >> quant >> cost >> mkt)
if (deptnum == x)
extcost = quant * cost;
extmkt = quant * mkt;
outFile << left << "n " << setw(7)<< item << " "
<< right << setw(4)<< quant << " "
<< right << setw(4) << cost << ".00 "
<< right << setw(3) << mkt << ".00 "
<< right << setw(5) << extcost<< ".00 "
<< right << setw(5) << extmkt << ".00";
totalcost += extcost;
totalmkt += extmkt;
if (totalcost > totalmkt)
lowcost = totalmkt;
else
lowcost = totalcost;
else
break;
outFile << right << "n Totalttttt $" << totalcost << ".00 $"
<< totalmkt << ".00 $"<< lowcost << ".00";
x += 1;
totalInv += lowcost;
outFile << "nTotal Inventorytttttt $"<< totalInv<< ".00";
inFile.close ();
outFile.close ();
return 0;
What is wrong with my logic?
c++ file-io
2
Now is an excellent opportunity for you to learn how to use your debugger. Your debugger allows you to run your program one line at a time; inspect the values of all variables, as they change; and observe your program's logical behavior, and why it does exactly what it does. Knowing how to effectively use a debugger is a required skill for every C++ developer. The logical flaw in the shown code appears to be simple, and I'm sure that once you use your debugger to trace how each line of the input line gets read and what happens to it, you'll figure it out in no time at all. Good luck.
– Sam Varshavchik
Nov 14 '18 at 4:01
Many thanks to @Sam Varshavchik for mentioning the degugger, It's so useful i can't believe my teacher didn't even mention it in class.
– Kyle Bading
Dec 9 '18 at 18:42
add a comment |
Why is the program skipping the first line of every new deptnum?
I am trying to read from a file that looks like:
1 Suits 0300 100 092
1 Coats 0200 060 065
1 Shirts 1000 012 013
2 Dresses 0400 060 065
2 Coats 0185 184 200
2 Shoes 0600 040 030
3 Jeans 0200 040 035
3 Shoes 0200 030 034
4 Jeans 0300 042 043
The deptnum is the first column.
And when I write to the other file I get:
Blinn Discount Apparel Company
Inventory Evaluation
10/12/2018
Unit Cost Extended
Quantity Cost Market Cost Market Lower Cost
Mens Dept
Suits 300 100.00 92.00 30000.00 27600.00
Coats 200 60.00 65.00 12000.00 13000.00
Shirts 1000 12.00 13.00 12000.00 13000.00
Total $54000.00 $53600.00 $53600.00
Womens Dept
Coats 185 184.00 200.00 34040.00 37000.00
Shoes 600 40.00 30.00 24000.00 18000.00
Total $112040.00 $108600.00 $108600.00
Girls Dept
Shoes 200 30.00 34.00 6000.00 6800.00
Total $118040.00 $115400.00 $115400.00
Boys Dept
Total $118040.00 $115400.00 $115400.00
Total Inventory $393000.00
It skipped Womens Dept -> Dresses, Girls Dept -> Jeans, and Boys Dept -> Jeans.
Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
ifstream inFile;
ofstream outFile;
int x = 1, deptnum, quant,cost,mkt,extcost,extmkt,totalcost = 0,totalmkt = 0,
lowcost,totalInv = 0;
char item [15];
inFile.open("blinn.dat");
outFile.open("blinn.dout");
if (!inFile)
cout <<"ntt Can't open data file: blinn.datn";
else
outFile <<"nt Blinn Discount Apparel Companyn";
outFile <<"t Inventory Evaluationn";
outFile <<"t 10/12/2018n";
outFile <<"ntttttt Unit Costttt Extendedn";
outFile <<"tt Quantity Cost Market Cost Market Lower Cost";
while (x < 5)
if (x == 1)
outFile << "nMens Dept";
else if (x == 2)
outFile << "nWomens Dept";
else if (x == 3)
outFile << "nGirls Dept";
else if (x == 4)
outFile << "nBoys Dept";
else
break;
while (inFile >> deptnum >> item >> quant >> cost >> mkt)
if (deptnum == x)
extcost = quant * cost;
extmkt = quant * mkt;
outFile << left << "n " << setw(7)<< item << " "
<< right << setw(4)<< quant << " "
<< right << setw(4) << cost << ".00 "
<< right << setw(3) << mkt << ".00 "
<< right << setw(5) << extcost<< ".00 "
<< right << setw(5) << extmkt << ".00";
totalcost += extcost;
totalmkt += extmkt;
if (totalcost > totalmkt)
lowcost = totalmkt;
else
lowcost = totalcost;
else
break;
outFile << right << "n Totalttttt $" << totalcost << ".00 $"
<< totalmkt << ".00 $"<< lowcost << ".00";
x += 1;
totalInv += lowcost;
outFile << "nTotal Inventorytttttt $"<< totalInv<< ".00";
inFile.close ();
outFile.close ();
return 0;
What is wrong with my logic?
c++ file-io
Why is the program skipping the first line of every new deptnum?
I am trying to read from a file that looks like:
1 Suits 0300 100 092
1 Coats 0200 060 065
1 Shirts 1000 012 013
2 Dresses 0400 060 065
2 Coats 0185 184 200
2 Shoes 0600 040 030
3 Jeans 0200 040 035
3 Shoes 0200 030 034
4 Jeans 0300 042 043
The deptnum is the first column.
And when I write to the other file I get:
Blinn Discount Apparel Company
Inventory Evaluation
10/12/2018
Unit Cost Extended
Quantity Cost Market Cost Market Lower Cost
Mens Dept
Suits 300 100.00 92.00 30000.00 27600.00
Coats 200 60.00 65.00 12000.00 13000.00
Shirts 1000 12.00 13.00 12000.00 13000.00
Total $54000.00 $53600.00 $53600.00
Womens Dept
Coats 185 184.00 200.00 34040.00 37000.00
Shoes 600 40.00 30.00 24000.00 18000.00
Total $112040.00 $108600.00 $108600.00
Girls Dept
Shoes 200 30.00 34.00 6000.00 6800.00
Total $118040.00 $115400.00 $115400.00
Boys Dept
Total $118040.00 $115400.00 $115400.00
Total Inventory $393000.00
It skipped Womens Dept -> Dresses, Girls Dept -> Jeans, and Boys Dept -> Jeans.
Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
ifstream inFile;
ofstream outFile;
int x = 1, deptnum, quant,cost,mkt,extcost,extmkt,totalcost = 0,totalmkt = 0,
lowcost,totalInv = 0;
char item [15];
inFile.open("blinn.dat");
outFile.open("blinn.dout");
if (!inFile)
cout <<"ntt Can't open data file: blinn.datn";
else
outFile <<"nt Blinn Discount Apparel Companyn";
outFile <<"t Inventory Evaluationn";
outFile <<"t 10/12/2018n";
outFile <<"ntttttt Unit Costttt Extendedn";
outFile <<"tt Quantity Cost Market Cost Market Lower Cost";
while (x < 5)
if (x == 1)
outFile << "nMens Dept";
else if (x == 2)
outFile << "nWomens Dept";
else if (x == 3)
outFile << "nGirls Dept";
else if (x == 4)
outFile << "nBoys Dept";
else
break;
while (inFile >> deptnum >> item >> quant >> cost >> mkt)
if (deptnum == x)
extcost = quant * cost;
extmkt = quant * mkt;
outFile << left << "n " << setw(7)<< item << " "
<< right << setw(4)<< quant << " "
<< right << setw(4) << cost << ".00 "
<< right << setw(3) << mkt << ".00 "
<< right << setw(5) << extcost<< ".00 "
<< right << setw(5) << extmkt << ".00";
totalcost += extcost;
totalmkt += extmkt;
if (totalcost > totalmkt)
lowcost = totalmkt;
else
lowcost = totalcost;
else
break;
outFile << right << "n Totalttttt $" << totalcost << ".00 $"
<< totalmkt << ".00 $"<< lowcost << ".00";
x += 1;
totalInv += lowcost;
outFile << "nTotal Inventorytttttt $"<< totalInv<< ".00";
inFile.close ();
outFile.close ();
return 0;
What is wrong with my logic?
c++ file-io
c++ file-io
asked Nov 14 '18 at 3:52
Kyle BadingKyle Bading
133
133
2
Now is an excellent opportunity for you to learn how to use your debugger. Your debugger allows you to run your program one line at a time; inspect the values of all variables, as they change; and observe your program's logical behavior, and why it does exactly what it does. Knowing how to effectively use a debugger is a required skill for every C++ developer. The logical flaw in the shown code appears to be simple, and I'm sure that once you use your debugger to trace how each line of the input line gets read and what happens to it, you'll figure it out in no time at all. Good luck.
– Sam Varshavchik
Nov 14 '18 at 4:01
Many thanks to @Sam Varshavchik for mentioning the degugger, It's so useful i can't believe my teacher didn't even mention it in class.
– Kyle Bading
Dec 9 '18 at 18:42
add a comment |
2
Now is an excellent opportunity for you to learn how to use your debugger. Your debugger allows you to run your program one line at a time; inspect the values of all variables, as they change; and observe your program's logical behavior, and why it does exactly what it does. Knowing how to effectively use a debugger is a required skill for every C++ developer. The logical flaw in the shown code appears to be simple, and I'm sure that once you use your debugger to trace how each line of the input line gets read and what happens to it, you'll figure it out in no time at all. Good luck.
– Sam Varshavchik
Nov 14 '18 at 4:01
Many thanks to @Sam Varshavchik for mentioning the degugger, It's so useful i can't believe my teacher didn't even mention it in class.
– Kyle Bading
Dec 9 '18 at 18:42
2
2
Now is an excellent opportunity for you to learn how to use your debugger. Your debugger allows you to run your program one line at a time; inspect the values of all variables, as they change; and observe your program's logical behavior, and why it does exactly what it does. Knowing how to effectively use a debugger is a required skill for every C++ developer. The logical flaw in the shown code appears to be simple, and I'm sure that once you use your debugger to trace how each line of the input line gets read and what happens to it, you'll figure it out in no time at all. Good luck.
– Sam Varshavchik
Nov 14 '18 at 4:01
Now is an excellent opportunity for you to learn how to use your debugger. Your debugger allows you to run your program one line at a time; inspect the values of all variables, as they change; and observe your program's logical behavior, and why it does exactly what it does. Knowing how to effectively use a debugger is a required skill for every C++ developer. The logical flaw in the shown code appears to be simple, and I'm sure that once you use your debugger to trace how each line of the input line gets read and what happens to it, you'll figure it out in no time at all. Good luck.
– Sam Varshavchik
Nov 14 '18 at 4:01
Many thanks to @Sam Varshavchik for mentioning the degugger, It's so useful i can't believe my teacher didn't even mention it in class.
– Kyle Bading
Dec 9 '18 at 18:42
Many thanks to @Sam Varshavchik for mentioning the degugger, It's so useful i can't believe my teacher didn't even mention it in class.
– Kyle Bading
Dec 9 '18 at 18:42
add a comment |
1 Answer
1
active
oldest
votes
There is a problem with your logic:
if (deptnum == x)
// do something
else
break;
To achieve the else
branch, you have already read a line that deptnum != x
(the first line of every new deptnum
) so at the next iterator, the current line is discarded by the next input line.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292958%2fc-file-i-o-skipping-line-every-new-iteration-why%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a problem with your logic:
if (deptnum == x)
// do something
else
break;
To achieve the else
branch, you have already read a line that deptnum != x
(the first line of every new deptnum
) so at the next iterator, the current line is discarded by the next input line.
add a comment |
There is a problem with your logic:
if (deptnum == x)
// do something
else
break;
To achieve the else
branch, you have already read a line that deptnum != x
(the first line of every new deptnum
) so at the next iterator, the current line is discarded by the next input line.
add a comment |
There is a problem with your logic:
if (deptnum == x)
// do something
else
break;
To achieve the else
branch, you have already read a line that deptnum != x
(the first line of every new deptnum
) so at the next iterator, the current line is discarded by the next input line.
There is a problem with your logic:
if (deptnum == x)
// do something
else
break;
To achieve the else
branch, you have already read a line that deptnum != x
(the first line of every new deptnum
) so at the next iterator, the current line is discarded by the next input line.
answered Nov 14 '18 at 4:13
mrbluemrblue
634
634
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292958%2fc-file-i-o-skipping-line-every-new-iteration-why%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Now is an excellent opportunity for you to learn how to use your debugger. Your debugger allows you to run your program one line at a time; inspect the values of all variables, as they change; and observe your program's logical behavior, and why it does exactly what it does. Knowing how to effectively use a debugger is a required skill for every C++ developer. The logical flaw in the shown code appears to be simple, and I'm sure that once you use your debugger to trace how each line of the input line gets read and what happens to it, you'll figure it out in no time at all. Good luck.
– Sam Varshavchik
Nov 14 '18 at 4:01
Many thanks to @Sam Varshavchik for mentioning the degugger, It's so useful i can't believe my teacher didn't even mention it in class.
– Kyle Bading
Dec 9 '18 at 18:42