0

I have downloaded the code as shown in the demo. I am running 'make debug' after 'cd debug' and I do not understand why the code below is receiving the "debug.c:9:5: error: use of undeclared identifier 'name'" upon compilation. This use of syntax appears differently than in a typical C program, for example, when using char str[50], scanf("%s", &str), and printf("Name: %s", str). Would you please provide me with a hint(as to where I am going wrong) and explain the difference between the typical syntax used in C versus the cs50 syntax that is used below?

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    string name, location;
    // Ask for your name and where live
    name = get_string("What is your name? ");
    location = get_string("Where do you live? ");

    // Say hello
    printf("Hello, %s, from %s!", name, location);
}

End of Question

Beginning of Reply

Please see screenshot of latest attempt to compile debug.c program. I am still receiving an error message despite including 'cs50.h' and 'stdio.h' within the header as included instructions for the compiler. Please reply back with any recommendation that you think would work to correct the issue.

enter image description here

2 Answers 2

1

First, understand that the "string" data type is a custom creation of the CS50 class. It's not a standard C data type. It's just been implemented to make things easier during the first couple of weeks and assignments to get new programmers going quickly.

Can't explain exactly why you're getting this error, but I have a feeling it's related to the fact that the code is declaring the two string vars in one place and then trying to populate them later.

The problem with strings (and with char arrays, as you'll learn later) is that they are what are known as immutable objects. That means that the space for them is allocated when they are first declared and can't be changed later. (You can destroy and recreate them with a different size, but you can't just make them longer.) So, by declaring them without initializing them, this code creates two string vars of length 0.

Instead, try declaring and populating them in the same line of code. That way, the correct amount of memory will be allocated (the actual number of characters, plus 1 to hold the end of string marker, '\0', aka, 0x00.)

In more specific terms, remove the initial line string name, location; and try this:

string name = get_string("What is your name? ");

along with the obvious similar code adjustments that will be needed.

This may clear your issue. The other possibility is that the CS50.h library isn't being loaded. It would help if you added a screenshot of the build attempt and the error.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

0

Thanks for the help. I have completed the practice exercise. I found that the mistake was that I was not in the correct folder on the cs50 vs code integrated development environment(IDE). I found the confusion on my part was that I typically do not complete exercises within this IDE. Please see completed solution below.

enter image description here

1
  • This is not an answer and should not be accepted as the answer, the other answer should be accepted. This was NOT the problem that caused the failure. The folder name your code is in is not relevant.
    – UpAndAdam
    Commented Dec 13, 2023 at 20:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .