There are a few big problems with your code.
First of all, the syntax for (float i = 0; i > 10, i++;)
is completely wrong. It compiles, but that's just a coincidence. The different command in the for loop control structure should be separated by semicolons (;
), not commas (,
). The correct code would then be for (float i = 0; i > 10; i++)
. By the way, you made a typo, I think you meant for (float i = 0; i < 10; i++)
(otherwise the for loop never runs since i
is initialized to 0 and 0 > 10
is false from the beginning).
Second of all, you're initializing the variable i
twice: once with float i;
and once in the for loop. That shouldn't compile, although with some compilers it does. There are two options on how to do. The first option is to declare the variable outside of the for loop and just assign it without initializing it in the for loop:
float i;
for(i = 0; i < 10; i++){
//some stuff
}
The second option is to simply declare it in the for loop as you did in the first loop:
for(float i = 0; i < 10; i++){
//some stuff
}
Another mistake that you made is to declare i
as a float
and then try to access dotName[i]
. Whatever you put inside the brackets has to be of type int
or something similar (unsigned int
, long
, etc). Putting a float
variable inside those brackets won't compile just like that. If you want to index an array with a float
, you need to tell the compiler that you want to convert it to an int
like this: dotName[(int)i]
or dotName[int(i)]
. This is called a cast. However, in your case, I would recommend just declaring i
as an int
.
Also, float(dotX < 10);
is completely wrong, I don't really understand what you're trying to do there. I think you meant to do float(dotX[i] < 10);
, but that still doesn't make any sense. What you would be doing there would be converting a bool
to a float
and then doing nothing with the result. That compiles and isn't wrong, but is completely useless. As I said, I don't understand what you want to do there.
Also, broi = broi++;
is correct but useless. broi++;
is enough. The ++
operator increments broi
by one by itself and then returns the result. What the ++
operator does internally is basically this:
int operator++(int &x){
x = x + 1;
return x;
}
So it already increments the variable automatically without you having to do anything. What you did is the same as doing this:
broi = broi + 1;
broi = broi;
Here, the first line represents the ++
operator and the second line represents the =
operator. It's clear that the second line is useless, so you can just remove it. In the same way, in your code, you can remove broi =
, leaving simply broi++;
.
You also did a few things that aren't recommended, but work just fine since the C++ standard supports them.
First of all, using namespace std;
is bad practice. It's recommended to omit it and add std::
in front of cin
, cout
and endl
. If you want to know why using namespace std;
is bad practice, it's well explained here. However, I must admit that I personally still use using namespace std;
since I think it's simpler.
Second of all, the main
function is supposed to return 0, so it's recommended to add return 0;
at the end of the main
function. The return value of the main
function tells what made the program close. The value 0 means that the program closed when it was supposed to. Any other values mean that the program crashed. A complete list of what each return value means is available here. Note that C++ supports omitting return 0;
and most compilers add it automatically if it is omitted, but it's still recommended to have it. Also, C doesn't support omitting return 0;
and in C it will return whatever happens to be in the memory, making it looked like the program crashed when it ended normally.
Also, #include <stdio.h>
is C and although it works in C++, it's not recommended. In C++, it's better to use #include <cstdio>
. All standard libraries that end with .h
in C can be used in C++ by removing .h
and adding a c
at the beginning. That's also the case with cmath
: in C, it would be #include <math.h>
and in C++, it's #include <cmath>
.
A good version of your code would therefore be:
#include <iostream>
#include <cmath>
#include <cstdio>
int main(){
int dotX[23]; //tri masiva
int dotY[23];
char dotName[23];
for (int i = 0; i < 23; i++){ // Cikal za vavejdane na masivite
std::cout << "Abscisa \t" << i + 1 << std::endl;
std::cin >> dotX[i];
std::cout << "Ordinata \t" << i + 1 << std::endl;
std::cin >> dotY[i];
std::cout << "Ime na tochkata" << std::endl;
std::cin >> dotName[i];
if (i >= 1){ //IF operatora i cikula za obhozhdane na masiva i presmqtane na distanciite
bool flag = true;
while (flag){
double distance = sqrt(pow(dotY[i] - dotY[i - 1], 2) + pow(dotX[i] - dotX[i - 1], 2)); //Formula za presmqtane na razstoqniqta
if (distance <= 6) {
char broi;
broi++;
std::cout << "abscisa \t" << i + 1 << std::endl;
std::cin >> dotX[i];
std::cout << "ordinata \t" << i + 1 << std::endl;
std::cin >> dotY[i];
}
else{
flag = false;
}
}
}
}
for (int i = 0; i < 10; i++){
float(dotX[i] < 10); //Note that I don't understand what you're trying to do here, so I just changed it to something that compiles
std::cout << dotName[i] << std::endl;
}
}