if/else statement for an input file

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;

Num_Rec_Output << mag << "\t" << direction << "\t"
<< f1 << "\t" << c1 << "\t" << f2 << "\t" << c2 << "\n";
}
else
{
std::cout << "Bad Numbers\n";
Last edited on
If you could use code tags it would sure make this easier to read.

All I can really tell is your code is incomplete without closing brackets.

I'm assuming your code doesn't work but I"m not sure there is enough here to tell why.
Maybe if you ask a question that would help....
More specifically where would i put the if/else statement inside main, after my ouput content or before?
Agree with Samuel. Can you post more of your code? with [Source code format]
Otherwise it would be as Samuel suggests.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;

Num_Rec_Output << mag << "\t" << direction << "\t"
<< f1 << "\t" << c1 << "\t" << f2 << "\t" << c2 << "\n";
}
else
{
std::cout << "Bad Numbers\n";
}
}

Last edited on
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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;
}
Last edited on
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


and thanks for all this help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>

struct literal_char_t {
  char expected = '\0';
};

std::istream &operator>>(std::istream &s, literal_char_t const &l) {
  char input;

  if (!(s >> input))
    return s;
  if (input != l.expected)
    s.setstate(std::ios::failbit);

  return s;
}

literal_char_t literal_char(char expected) { return literal_char_t{expected}; }

struct vec2 {
  double x = 0.0, y = 0.0;
};

double dot(vec2 v, vec2 u) { return v.x * u.x + v.y * u.y; }

double arg(vec2 v) { return std::atan2(v.y, v.x); }

double norm(vec2 v) { return std::sqrt(dot(v, v)); }

std::istream &get_vec2(std::istream &s, vec2 &v) {
  return s >> v.x >> std::ws >> literal_char('i') >> v.y >> std::ws >>
         literal_char('j');
}

std::ostream &print_vec2(std::ostream &s, vec2 const &v) {
  return s << '<' << v.x << ", " << v.y << ">:\n\t"
           << "arg: " << std::setw(12) << arg(v) << "\n\t"
           << "norm:" << std::setw(12) << norm(v) << '\n';
}

bool more_junk_in_stream(std::istringstream &s) {
  return s >> std::ws && !s.eof();
}

int main() {
  for (std::string line; std::getline(std::cin, line);) {
    std::istringstream vec2_line{line};

    vec2 v;
    if (get_vec2(vec2_line, v) && !more_junk_in_stream(vec2_line))
      print_vec2(std::cout, v);
    else
      std::cerr << "ill-formed record:\n\t" << std::quoted(line) << '\n';
  }
}


Live demo:
http://coliru.stacked-crooked.com/a/c0fe9c60e3c07c74
Last edited on
Topic archived. No new replies allowed.