0

During Problem Set i came across the error Expected Expression on the statement "string s = GetString();" Suggestions would be appreciated. The code is provided below.

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

int main(void)
{
    int s = 0; // Height
    int t = 0; // Line
    int u = 0; // Column

    do
    {
        // Gathers string.
        printf("Please provide the height of the pyramid: ");
        int s = GetInt(void);
    }
    while (s < 1 || s > 23);

    // Selects a line.
    for (int t = 1; t <= s; t++)
    {
        // Selects a column.    
        for (int u = 1; u <= s; u++) 
        {
        // Prints character.
        if (u >= s + 1 - t)
        {
            printf("#");
        }
        else
        {
            printf(" ");
        }
        }
    }
    // Prints next line.
    printf("\n");
}
1
  • Where have you used GetString() in the program? Am I missing anything? I do not understand where the error is generated.
    – AviB
    May 23 '15 at 8:46
1

Change GetInt(void) to GetInt(). In C, void is a type that of an object with no type. Calling GetInt(void) generates an error because type identifiers (Ex: int, char, double) are not passed into a function; variables are. When GetInt is declared as void GetInt(void), the void is included to signal that nothing will be passed into the function as a parameter.

This is not directly related to the error, but in the line int s = GetInt();, the type identifier int should be omitted since s has already been declared. Including int declares a new variable named s whose scope is inside the braces between do and while. This will cause the s declared in main to remain 0 as the result of GetInt() is placed in a variable that only exists inside the while loop.

1
  • Thanks for your help, i have successfully patched the errors.
    – user7194
    May 24 '15 at 2:25

You must log in to answer this question.