Hi..Im currently developing a program that will store voltage N current into an csv file..In my program,when the user inputs -100 for voltage it will terminate the program..However,in the output csv file, the value -100 appears as well..I dont want it to appear on my csv file..How do I get rid of this last row(the row with voltage=-100) in my csv file? Here is my code :
Line 14: a is unititialized. First time through the loop, results are unpredictable.
Line 22: You need to test for -100 before you write to the outdata file.
Line 24: This will open the file every time through the loop.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Also, please get into the habit of applying meaningful names to your variables. In tiny applications like this it doesn't matter so much, but it will help you in the future if you plan to ever write anything larger.
Line 8: 'd' is not being used and there is no fourth variable needed in this equation set. Total resistance can be calculated given the variables present.
AbstractionAnon (1777): ok..Now I have initialized (all a,b,c)=0 and placed indata.open("out.csv"); in the main before system("pause");..
How do I test for -100 before I write to the outdata file..I dont get you.
When -100 is entered for a, you want to skip the rest of the loop. There are a couple of ways to do this:
Using a break statement:
1 2 3 4 5 6 7 8 9 10
while (a !=-100)
{ cout<<"Enter voltage:";
cin>>a;
if (a == -100)
break; // Exit the loop
cout<<"Enter current:";
cin>>b;
c=a*b;
outdata<<a<<","<<b<<","<<c<< endl;
}
Using an if statement:
1 2 3 4 5 6 7 8 9 10
while (a !=-100)
{ cout<<"Enter voltage:";
cin>>a;
if (a != -100)
{ cout<<"Enter current:";
cin>>b;
c=a*b;
outdata<<a<<","<<b<<","<<c<< endl;
}
}