code not working

my code is not taking 2nd character input. i don't understand what is the problem with my code. plzz help me?
here is the code..


#include<stdio.h>

int main()
{
char ms,sex;
int age;
printf("enter your marital status M/UM\n");
scanf("%c",&ms);
printf("enter your age\n");
scanf("%d",&age);
printf("enter your sex\n");
scanf("%c",&sex);
if(ms==M)
printf("driver will be insured");
else if
{
if(sex==m&&age>=30)
printf("driver will be insured");
else if(sex==f&&age>=25)
printf("driver will be insured");
}
else
printf("driver won't be insured");
return 0;
}

i'm using dev c++ compiler
i'm using dev c++ compiler

...but you are writing C-style code...

This code works:
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
#include <stdio.h>

int main()
{
    printf("\nEnter your marital status M/UM: ");
    char ms;
    scanf("%c", &ms);

    printf("\nEnter your age: ");
    int age;
    scanf("%d", &age);

    printf("\nEnter your sex: ");
    char sex;
    scanf(" %c", &sex);

    if(ms == 'M')
    {
        printf("\nDriver will be insured");
    }
    else if(sex == 'm' && age>=30)
    {
        printf("\nDriver will be insured");
    }
    else if (sex == 'f' && age>=25)
    {
        printf("\nDriver will be insured");
    }
    else
    {
        printf("\nDriver won't be insured");
    }

    printf("\n");

    return 0;
}


You can find explanations here:
http://stackoverflow.com/questions/8464620/program-doesnt-wait-for-user-input-with-scanfc-yn

Please note that you declare a single char to store an answer that could be a sting (see the first printf(): proposed answers are 'M' and "UM", which is a string, not a single character).

Perhaps you could find better help in C forum - just a suggestion.
Hi there
I have tried to fix your program but because I don't know when you should have positive and when negative answer I can't fix it all.

there was lots things that could be corrected.

1. you didn't have #include <iostream> possibly becuse you did't need in your version of the code? (I didn't use printf and scanf.)

2. When you paste your code use tags to start "[" code "]" and to finish "[" /code "]" without quotes of course.

3. Don't compact your code (you are NOT ZIP ;P) white spaces are ignored in C++ so you can make extra space, it will make your code more readable.

4. if you select UM (unmarried) in your code there is nothing about this so it jumps directly to last else statement.

5. Statement like this if(sex==m&&age>=30), well not sure what more experienced developers will say about this but I don't like it. in this form. Its ok I guess, does not breaks rules, but I would prefer to add nested else if statement and ask for age inside. This gives you more options, not only 30 years, other ranges.

6. Char should be in single quotes
not like this if(ms==M) but like this if(ms == 'M')

As for your IDE you can use
http://www.codeblocks.org
or Microsoft Visual Studio

I hope that will help you :)



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
#include<stdio.h>
#include <iostream>
using namespace std;

int main()
{
    char ms,sex;
    int age;
    
    cout << "enter your marital status M/UM: ";
    cin >> ms;
    cout << "enter your age: ";
    cin >> age;
    cout << "enter your sex: ";
    cin >> sex;

    if (ms =='M' || ms == 'm')
    {
        cout << "driver will be insured";
    }
    else if (sex =='m' && sex == 'M')
    {   
        if (age >= 30)
        {
        cout  << "driver will be insured";
        }
        else
        {
            cout << "Sorry mate!";
        }
    }
    else if (sex == 'f' && age >= 25)
    {
        cout << "driver will be insured";
    }
    else
    {
        cout << "driver won't be insured";
    }
return 0;

}
Topic archived. No new replies allowed.