Shruder
Shruder

Reputation: 5

uninitialized local variable used C ++

I have rewritten the char a, b, c, d and f as int and then assigned them values and I am still getting the error

C4700 uninitialized local variable used. 

How can I fix this? I want the StudentsGrade = a; to be returned if the conditions are true.

#include "stdafx.h"

#include<iostream>
using namespace std;

int main()
{

    char a; 
    char b; 
    char c; 
    char d; 
    char f; 

    char students;  
    double totalpoints = 0;
    char  StudentsGrade; 


        cout << "Enter Student's name"; 
        cin >> students; 

        if (totalpoints > '360' && totalpoints < '400')
            StudentsGrade = a;
        else if
            (totalpoints > '320' && totalpoints < '359')
            StudentsGrade = b;
        else if
            (totalpoints > '280' && totalpoints < '319')
            StudentsGrade = c;
        else if
            (totalpoints > '240' && totalpoints < '279')
            StudentsGrade = d;
        else
            (totalpoints < '240');
            StudentsGrade = d;

    return 0;
}

Upvotes: 0

Views: 1711

Answers (3)

Lir Dulce
Lir Dulce

Reputation: 16

I see what your problem is. You haven't initialized your char variables.

char a = 'a'; 
char b = 'b'; 
char c = 'c'; 

Upvotes: 0

M Umer
M Umer

Reputation: 413

you only need to do the following changes, initialize char variables with actual value like char a = 'a'

char a = 'a'; 
char b = 'b'; 
char c = 'c'; 
char d = 'd'; 
char f = 'f';

and you also have some more errors, you're using d twice.

Upvotes: 1

BobMorane
BobMorane

Reputation: 4296

In your code, a is a variable, not the actual letter a. Try with this:

char a = 'a';   // Now the variable a (which is of type char) contains the character a.

for all your chars, a to f. This initializes the variables. You could also remove completely the variables, like so:

#include "stdafx.h"

#include<iostream>
using namespace std;

int main()
{
    char students;  
    double totalpoints = 0;
    char  StudentsGrade; 

        cout << "Enter Student's name"; 
        cin >> students; 

        if (totalpoints > 360 && totalpoints < 400)
            StudentsGrade = 'a'; // <-- Notice here!
        else if
            (totalpoints > 320 && totalpoints < 359)
            StudentsGrade = 'b'; // <-- And so on...
        else if
            (totalpoints > 280 && totalpoints < 319)
            StudentsGrade = 'c';
        else if
            (totalpoints > 240 && totalpoints < 279)
            StudentsGrade = 'd';
        else
            (totalpoints < 240);
            StudentsGrade = 'd';

    return 0;
}

I think that's what you intended to do in the first place. Finally, notice the numbers (in the conditions) have no more '' around them.

Upvotes: 2

Related Questions