I set up the code to read the file, which has 12 lines of xi, and yj components to give magnitude and direction, some of them are incorrect (on purpose) and i want to create an if/else statement that'll return "Bad Numbers" for the incorrect numbers.
The incorrect numbers don't fit the requirement of a having one i and one j (c1, c2), so i was thinking in the if/else statement the c1= i and c2=j
thanks
Num_Rec_File >> Counter1;
std::cout << "\nNumber of records found: " << Counter1 << "\n";
std::cout << std::fixed << std::setprecision(2);
for (int i = 0;i < (Counter1);i++) //
{
Num_Rec_File >> f1 >> c1 >> f2 >> c2;
mag = std::sqrt(f1*f1+f2*f2);
direction = atan2(f2,f1);
if (c1 == i)
{
std::cout << mag << "\t" << direction << "\t"
<< f1 << "\t" << c1 << "\t" << f2 << "\t" << c2 << "\n";
return 1;
You could make a function that checks the "counter" and then call the function inside your main. Can you post more of your code? would help (me) to solve your problem
#include <iostream> //cout and cin
#include <fstream> // ifstream and ofstream
#include <string> // string
#include <iomanip> // setprecision
#include <cmath>
int main()
{
// Part 1, file with number of lines in first line.
std::ifstream Num_Rec_File;
std::ofstream Num_Rec_Output;
char Input_File[20];
char Output_File[20];
int Counter1, i1;
float f1, f2, mag, direction;
char c1, c2;
std::cout << "Please enter the name of the input file"
<< " with first line showing\nnumber of records:\n";
std::cin >> Input_File;
std::cout << "\nPlease enter the name of the output file for these data:\n";
std::cin >> Output_File;
Num_Rec_File.open(Input_File);
if (Num_Rec_File.fail())
{
std::cout << "\n" << Input_File << " was not able to be opened for reading\n";
return 1;
}
else
{
std::cout << "\n" << Input_File << " was opened for reading\n";
}
Num_Rec_Output.open(Output_File);
if (Num_Rec_Output.fail())
{
std::cout << "\n" << Output_File << " was not able to be opened for reading\n";
return 1;
}
else
{
std::cout << "\n" << Output_File << " was opened for reading\n";
}
Num_Rec_File >> Counter1;
std::cout << "\nNumber of records found: " << Counter1 << "\n";
std::cout << std::fixed << std::setprecision(2);
for (int i = 0;i < (Counter1);i++) //Note that later you may want to use while to include error checking
{
Num_Rec_File >> f1 >> c1 >> f2 >> c2;
mag = std::sqrt(f1*f1+f2*f2);
direction = atan2(f2,f1);
if (c1 == i)
{
std::cout << mag << "\t" << direction << "\t"
<< f1 << "\t" << c1 << "\t" << f2 << "\t" << c2 << "\n";
return 1;
Num_Rec_Output << mag << "\t" << direction << "\t"
<< f1 << "\t" << c1 << "\t" << f2 << "\t" << c2 << "\n";
}
else {
std::cout << "Bad Numbers\n";
}
}
Num_Rec_File.close();
Num_Rec_Output.close();
return 0;
}
input file is just
12
10.2 i 37.5 j
12.3 i 23.5 j
12 i 23.3 j
1.23 i 23.4 j
0.12 i 0.034 j
0 i 43.5 j
34.4 j 0 j
53.0 i 90.3 k
34.3 j 2.34 j
34.4 i 34.3 j
12.1 j 12.1 j
12.1 i