#"Identifier is undefined"

1 messages · Page 1 of 1 (latest)

woeful pilot
#

My code gives me the error "Identifier is undefined" and I have no clue why

slate egretBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

woeful pilot
#
{
    int TotalGrade = 1000;
    int grade; 

    char Assignment(string assignment);  {

        cout << "Please enter the score for " << assignment;
        cin >> grade;
    }
    
    Assignment("Assignment #01");
    
    _getch();
        return 0;
}```
#

It's saying "assignment" is undefined

next shadow
#

You can't define a function inside another function

woeful pilot
#

you can't?

#

is that just a python thing lmao

next shadow
#

It depends on the language
C and C++ you can't (save for lambdas in c++)

woeful pilot
#

I get so confused with C and C++

#

if I take it out of main it just fills with tons of errors

next shadow
#

What is the error in question?

woeful pilot
next shadow
#

Can you share your updated code?

woeful pilot
#
int TotalGrade = 1000;
int grade; 

char Assignment(string assignment);  {

    cout << "Please enter the score for " << assignment;
    cin >> grade;
    }
    
Assignment("Assignment #01");
    
_getch();
    return 0;
next shadow
#

You still need a main function.
Code does not execute outside of a function

woeful pilot
#

I'm so confused, you said I can't define a function inside of it

next shadow
#

Put main around the code AFTER the Assignment function

woeful pilot
#
int TotalGrade = 1000;
int grade; 

char Assignment(string assignment); 
{

    cout << "Please enter the score for " << assignment;
    cin >> grade;
}
    
main() {

    Assignment("Assignment #01");

    _getch();
    return 0;
}
#

now I'm getting "Type char unexpected"

next shadow
#

Okay last thing, you don't need a ; between the function arguments and the body of Assignment's definition

#

Oh and you need main to return an int

woeful pilot
next shadow
#

Add the word int in front of main

woeful pilot
#

Oml finally thank you so much

#

python is so much easier 😭

knotty vale
#
  1. The Assignment function name is not good as it doesn't describe what the function does. Maybe input_grades could be used as it's name?
  2. Since grade is a integer value you should return int.
  3. The value returned from the function call is not being assigned to a variable.
  4. Not sure what the _getch() is for
woeful pilot
#

tbh our teacher puts _getch() in every single template but never told us what it's for

#

It doesn't run properly without it, gives me a bunch of weird stuff at the end of the console without it

woeful pilot
knotty vale
#

I get that but function names have to be descriptive

#

without looking at the implementation you should figure out what it does just by the name

woeful pilot
#

I mean it quite literally just replaces the assignment name

#

I have another question tho @knotty vale, How can I use the cin value to add to a variable instead of setting it

#
cout << "Please enter the score for " << assignment << ": ";
cin >> grade;
return 0;
#

I want cin to add to 'grade' instead of setting it

knotty vale
#

You would need an array or vector if I'm understanding what you're saying correctly

woeful pilot
#

Would you mind giving me an example?

knotty vale
#

You mean like tracking multiple student grades right?

woeful pilot
#

So the assignment is a grade calculator, I take the grades of multiple assignments and essentially output a prediction of how much I need to get certain grades

#

I'm wanting to make it so when you input the grade of an assignment, it adds that input to the "grade" variable

#

instead of setting it

knotty vale
#

If you know the specific amount of grades to input using an array is pretty good otherwise you can use a vector.

This is the example for an array

int grades[10];
for(int i = 0; i < grades.size(); i++){
std::cin >> grades[i];
}
slate egretBOT
#

@woeful pilot Has your question been resolved? If so, type !solved :)

woeful pilot
#

I must've accidentaly skipped something in my course lmao

knotty vale
#

I’m guessing for every assignment it lists the amount of students, so I’ll keep going with arrays

woeful pilot
#

It's not for multiple students

#

it's a personal grade calculator, only needs the one grade total

knotty vale
#

Can you post the assignment details cause if it’s a personal grade calculator you could do it all in the main function

woeful pilot
#

I figured it out lmao, incredibly simple

#

LIterally just created a second variable, and inside of the function added the "grade" variable to the "totalgrade"

#

it's an extra line of code than what I had intended but it works perfect

knotty vale
woeful pilot
#
{

    cout << "Please enter the score for " << assignment << ": ";
    cin >> grade;
    TotalGrade = grade + TotalGrade;
    return 0;
}```
knotty vale
#

Make the char Assignment_Grade return void if you don’t have to return anything