Whats wrong

Please i don´t know whats wrong, i need make a "FOR" but don´t do it.
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
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

char nom[50];
char sex;
int x, y, cont1 = 0, cont2 = 0;
float cl1, cl2, cl3, suma, prom;
char *nomh[]={};
float pm[]={};
float ph[]={};
main ()
{
for (x=1;x<3;x++)
{
    cout<<"\n Full Name: ";
//    gets(nom);
      fgets(nom,50,stdin);
    cout<<"\n <Women / Men >: ";
    cin>>sex;
         if ((sex == 'M')||(sex == 'm'))
         {
         nomh[x] = nom;
         cout<<"\n 1rs. qualification: ";
         cin>>cl1;
         cout<<"\n 2nd. qualification: ";
         cin>>cl2;
         cout<<"\n 3rs. qualification: ";
         cin>>cl3;
         suma = cl1 + cl2 + cl2;
         prom = suma / 3;
         ph[x] = prom;
         cont1 = cont1 + 1;
         }
system("cls");
}
for (y=0;y<cont1;y++)
{
cout<<"\n Studen: "<<nomh[y]<<" ** Average: "<<ph[y];
}
system("pause");
}


thanks.
If you do not know what is wrong then ask the compiler about this. It will point out what is wrong.
Last edited on
closed account (o3hC5Di1)
Hi there,

Could you please be a little bit more specific as to what is not working?
Also this line:

1
2
//line 37
system("cls");

is clearing the console window text during each iteration of x<3, possibly it works, but you can't see the output as you're clearing the screen after every iteration?

All the best,
NwN
you're right, once compiled and executed, the code is developed right the first time the cycle "for" but not the second time to capture the full name. It is there where I do not know what is wrong.

1
2
3
4
   cout<<"\n Full Name: ";
//    gets(nom);
      fgets(nom,50,stdin);

thanks.
Your code shall not be compiled.
thanks NwM.-
I clean the screen to the previous iteration not be seen by the user, in the end I get a summary of students and on-screen averages. But I fail.

vlad from moscow (1030)
Sorry but I can compile the code.
closed account (o3hC5Di1)
Hi Josan,

Try making nom a string and read it with regular cin maybe?

1
2
3
4
string nom;

cout<<"\n Full Name: ";
cin >> nom;


Hope that helps.

All the best,
NwN
@josan
vlad from moscow (1030)
Sorry but I can compile the code.


It is only because you uses the compiler incorrectly. You shall switch off C features because your program is C++.
Moreover your program is invalid in whole.
Last edited on
Like vlad says these lines of code
1
2
3
char *nomh[]={};
float pm[]={};
float ph[]={};

are going to mean you write over all sorts of memory that you shouldn't when you have this
ph[x] = prom;

Reserve some space for your arrays, if your compiler lets you reserve an array of 0 size, change it.
thank you for your support. I still have much to learn, perhaps due to start by explaining to do my program.

A. - Capture the full name of 20 students.
2. - Capture if male or female.
3. - Capture three grades each.
4. - Save in a list the average of each student.
5. - To display: Full name / sex / average.

thanks for everything.
closed account (o3hC5Di1)
Hi there,

Then you would need for (x=0;x<20;x++), currently you only have 3 iterations.

What the previous posters meant is that your code is using
- C syntax and libraries, not recommended in c++
- ambiguous variable declarations

If you also need to store the names of each student, you should use a string array: string names[20]; . If you don't need to store them, it's probably still the best to use a string rather than char[50].
As I said, using a string gives you the benefit of being able to use std::cin, in stead of fgets().

I could rewrite your objectives as following instructions:


Do the following for 20 students:
- Ask their name
- Ask if they are male or female
- Ask three grades and save the average for every student in a list
- Display the information in following format: Full name / sex / average.


Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.