Monday, March 14, 2022

[SOLVED] How do I make a Diary in C language?

Issue

First timer here; I'm trying to make a diary program in c following the cs50 reference

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

int main(void)

{

//Program ask user for their name
string name = GetString("What is your name?\n");
printf("Hello there %s, name\n");

//Program ask user for their age
int age = GetInt("What is your age?\n");
printf("You are %i, old\n");

}

When I run the code with clang, it returns this error message

Diarytest.c:10:25: error: too many arguments to function call, expected 0,
      have 1
string name = GetString("What is your name?\n");
              ~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:106:1: note: 'GetString' declared here
string GetString(void);
^
Diarytest.c:11:22: warning: more '%' conversions than data arguments
      [-Wformat]
printf("Hello there %s, name\n");
                    ~^
Diarytest.c:14:18: error: too many arguments to function call, expected 0,
      have 1
int age = GetInt("What is your age?\n");
          ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:87:1: note: 'GetInt' declared here
int GetInt(void);
^
Diarytest.c:15:18: warning: more '%' conversions than data arguments
      [-Wformat]
printf("You are %i, old\n");
                ~^
2 warnings and 2 errors generated.

Is there something that I'm missing here? I installed the cs50 library in my OS and I even tried linking it and it returned the same error message. Thanks for any help! Cheers.

Update: The Code was updated with some help from a User below (Thank you David Cullen) and now the code looks like this.

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

int main(void)

{

//Program ask user for their name
string name = GetString();
printf("Hello there, %s\n", name);

//Program ask user for their age
int age = GetInt();
printf("You are, %i\n", age);

}

However, when I run the program with clang I get a new error message.

/usr/bin/ld: /tmp/Diarytest-862733.o: in function `main':
Diarytest.c:(.text+0x9): undefined reference to `GetString'
/usr/bin/ld: Diarytest.c:(.text+0x2a): undefined reference to `GetInt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm stumped


Solution

Diarytest.c:10:25: error: too many arguments to function call, expected 0,
      have 1
string name = GetString("What is your name?\n");
              ~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:106:1: note: 'GetString' declared here
string GetString(void);
^

This error means you cannot pass "What is your name?\n" to GetString.

Diarytest.c:11:22: warning: more '%' conversions than data arguments
      [-Wformat]
printf("Hello there %s, name\n");
                    ~^

This error means you have to supply another parameter to printf here (maybe name).

Diarytest.c:14:18: error: too many arguments to function call, expected 0,
      have 1
int age = GetInt("What is your age?\n");
          ~~~~~~ ^~~~~~~~~~~~~~~~~~~~~
/usr/local/include/cs50.h:87:1: note: 'GetInt' declared here
int GetInt(void);
^

Similar to the above problem, you cannot pass "What is your age?\n" to GetInt.

Diarytest.c:15:18: warning: more '%' conversions than data arguments
      [-Wformat]
printf("You are %i, old\n");
                ~^
2 warnings and 2 errors generated.

Once again, you have to supply another parameter to printf here (maybe age).



Answered By - David Cullen
Answer Checked By - Clifford M. (WPSolving Volunteer)