The window close down before the process finish

Hi everyone, i was doing my homework, it's about a simple menu, and when I started to put the name, and other words, the window close down before the process finish

this is the code:

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
#include <stdio.h>
#include <conio.h>
#include <iostream.h>

main()
{
   
char name, carreer;
int  progra, calculus, mesurment;

printf("\n This program shows you the average of a student beteewn three subjects:");

printf("\n Programing, Integral calculus, and Meassurement of engeeniering");

printf("\n\n\n Enter the name of the student: ");

scanf("%i",&name);

printf("\n\n Enter the career of the student: ");

scanf("%i",&carreer);


getch();
}


Everything was going ok, until the second question appear

Enter the name of the student______
and if you pulse any button, the window close down, I don't know what's going on, can anybody help me?
Last edited on
You are not reading properly those variables. First of all, you don't need iostream.h header.

You declare the variables name and carrer with the type char - which stores just character.

For instance, if I declare: char x; in x - variable I can store data like that: x = 'a'; or x = '1'. Like you see, I store in it just a character. You want to store in them an entire name, which is composed from many characters, so you have to change the data-type. One data - type can be the array - char data type which is declared like so: char name[size_name];

On the other hand, in scanf function you are reading those values but you didn't chosen the appropriate modifier (that %i). I don't really know where it comes from but if you want to read a character, you would transform it like that: %c, if you read a string, it may be %s, if you read an integer you transform it like that %d.

We will use %s because, in fact, the name and carrer variables are strings.

This is the source:

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
#include <stdio.h>
#include <conio.h>


int main()
{

char name[10], carreer[10];
int  progra, calculus, mesurment;

printf("\n This program shows you the average of a student beteewn three subjects:");

printf("\n Programing, Integral calculus, and Meassurement of engeeniering");

printf("\n\n\n Enter the name of the student: ");

scanf("%s",&name);

printf("\n\n Enter the career of the student: ");

scanf("%s",&carreer);


getch();
}
Ok, thank you ^^

I already undersand
Topic archived. No new replies allowed.