Why so many error in function main ()?

So,i'm new and i'm wondering why i have so much errors about this code.I think that maybe i'm not using the right library or something.

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
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
	float suma=0.0;
	float sredno_aritmetichno=0.0;
	int broi_nulevi_chisla=0;
	float min,max;
	float P[N];
	int i;
	int izbor;
	int imax;
clrscr();
textbackground(WHITE);
textcolor(BLUE);
	
do {
	cin>>"nn Vavedete broi chisla <=25";
	cout<<"%d",&nt;
}while(nt<1 ||nt>25);
cin>>"nn Vavedete stoinosti za chislata ot masiva %d n",nt;
for(i=0;i<nt;i++)
do{
	cin>>"b N[%d] = ",i+1;
	cout<<"%p",&P[i];
}while(P[i]<-1000||P[1]>1000);
min=50.0;
max=-50.0;
for (i=0;i<nt;i++)
{
	if(P[i]<0||F[i]>0) suma+=P[i];
	else broi_nulevi_chisla++;
	if(P[i] <min) minP=[i];
	if(P[i] >max) maxP=[i];
	if (P[i>max]) 	{	max=P[i];  imax=i+1;}
	if(P[i]>=min&&P[i]<=max) {suma=suma+P[i];  nt++;}
	sredno_aritmetichno=suma/nt;
	do{   
clrscr();   
puts("n");   
puts("  Menu: ");   
puts(" 1. Izchislqvane na sredno arimetichno na chislata v intervala (min,max) ");  
puts(" 2. Izchislqvane na sumata ot razlichnite ot nula chisla  i broq na nulevite chisla");   
puts(" 3.Namirane na maksimalno polojitelno i negoviq poreden nomer");   
puts(" 4. Izhod");
	}
	do{   
cin>>("nt  Vashiqt izbor: ");   
cout<<("%d", &izbor);
}while (izbor<1||izbor>4);

}
Last edited on
Hello goshko2,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, 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/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

Just as a tip it is always helpful to include the error messages when you post your code. And do not leave it to someone else to figure out where the problem is, give some idea.

If the program reads a file include this file or at least a good sample so everyone is working with the same information.

I will load up the program and have a look.

Two things I do see for now is the use of C header files, the ones that end in ".h", are not needed.

A quick look through your code and I see you are trying to mix C and C++ code. This does when done right, but yo are doing it all wrong.

The big question is do you want to write a C program or a C++ program?

Hope that helps,

Andy
Hello goshko2,

Here is your program with a few fixes:

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
#include <iostream>
#include <stdlib.h>  // Not needed, but OK if you leave.
#include <conio.h>   // Not needed and not everyone can use.

using namespace std;  // Best not to use.

int main()
{
	float suma = 0.0;
	float sredno_aritmetichno = 0.0;
	int broi_nulevi_chisla = 0;
	float min, max;
	float P[N];
	int i;
	int izbor;
	int imax;
	clrscr();
	textbackground(WHITE);
	textcolor(BLUE);

	do
	{
		cin >> "nn Vavedete broi chisla <=25";
		cout << "%d", &nt;
	} while (nt < 1 || nt>25);

	cin >> "nn Vavedete stoinosti za chislata ot masiva %d n", nt;

	for (i = 0; i < nt; i++)
		do
		{
			cin >> "b N[%d] = ", i + 1;
			cout << "%p", &P[i];
		} while (P[i] < -1000 || P[1]>1000);

		min = 50.0;
		max = -50.0;

		for (i = 0; i < nt; i++)
		{
			if (P[i] < 0 || F[i]>0) suma += P[i];
			else broi_nulevi_chisla++;
			if (P[i] < min) minP = [i];
			if (P[i] > max) maxP = [i];
			if (P[i > max]) { max = P[i]; imax = i + 1; }
			if (P[i] >= min && P[i] <= max) { suma = suma + P[i]; nt++; }

			sredno_aritmetichno = suma / nt;
			do
			{

				clrscr();
				puts("n");
				puts(" Menu: ");
				puts(" 1. Izchislqvane na sredno arimetichno na chislata v intervala (min,max) ");
				puts(" 2. Izchislqvane na sumata ot razlichnite ot nula chisla i broq na nulevite chisla");
				puts(" 3.Namirane na maksimalno polojitelno i negoviq poreden nomer");
				puts(" 4. Izhod");
			}

			do
			{
				cin >> ("nt Vashiqt izbor: ");
				cout << ("%d", &izbor);
			} while (izbor < 1 || izbor>4);
                 }

I did put a few comment in the program for now, but what I want you to see is the layout of the code. With proper indenting and use of the {}s , as I have changed, you can easily see that the opening brace of main has no closing brace. When the {}s line up in the same column the {}s are easier to match. I am not saying that there is anything wrong with what you did it works, but what is above is easier to follow.

I will mention this early "conio.h" is an old header file that is no longer used by other IDEs/compilers. This tends to limit your code to your computer.

A "float" is OK, but not as accurate as a "double" and these days a "double is the preferred floating point type.

Some variables are initialized, but some are not. My personal preference is to initialize a variable when it is defined just to know that it does not contain a garbage value. Some variables the get a value quickly and do not need to be initialized.

From C++11 on you can define and initialize a variable as: int izbor{};. The empty {} will set the variable to zero.

Line 13. This is not allowed in C or C++. The "N" needs to be a constant number to define an array. And at this point "N" is not even defined.

Lines 17 - 19. Lead me to believe that you may be using a very old IDE/compiler as these functions are not available in C++.

Line 23. Will not work for input.

Line 24. You are mixing C++ and C code in the same line and that does not work. It should look more like: cout << nt;. Except that "nt" is an undefined variable.

Not sure if you realize it, but line 29 starts a for loop and line 30 is the only part of the for loop. Lines 36 on, although indented, are not part of the for loop. I suspect this indenting is because of the missing closing brace of "main".

After that there are some syntax errors, some from mixing C and C++ code the wrong way, undefined variables and misspelled variables.

I have not gone over all of the code yet to see all that needs fixed just some of the big things that I saw.

Hope that helps,

Andy
@Handy Andy
"conio.h" is an old header file that is no longer used by other IDEs/compilers.

That's not true, VS 2017 still has it.
@Thomas1965,

Sorry about that. This is true and I should have been more clear in my meaning. The intent was to say other than Microsoft.

Along the way I somehow lost the post and was left with a preview that was not up to date. I did not rewrite it as well as I did the first time.

Andy
Hello goshko2,

Here is a tip for you.

Comment out most of the code and work on one small piece at ta time. This will cut down on the number of errors to fix.

Just with the first do/while loop this shows what an uninitialized array will do.

Given the code:
1
2
3
4
5
6
do
{
	std::cout << "\n Enter number of numbers";
	std::cout << "\n Vavedete broi chisla <= 25: ";
	cin >> nt;
} while (nt < 1 || nt > 25);

The output is:

 Enter number of numbers
 Vavedete broi chisla <= 25: 3

 Enter numbers for array numbers
 Vavedete stoinosti za chislata ot masiva 3
-9.25596e+61:


The very large negative number is based on the garbage that the array is trying to use.

With an Initialized array it would look like this:

 Enter number of numbers
 Vavedete broi chisla <= 25: 3

 Enter numbers for array numbers
 Vavedete stoinosti za chislata ot masiva 3
0:


Although this is not really what you want.

Along with fixing the errors the program needs to be done differently.

Hope that helps,

Andy
Topic archived. No new replies allowed.