Questions tagged [cs50]

Code questions pertaining to Harvard's self-study introductory. It is STRONGLY RECOMMENDED to additionally tag with C or Python. Also consider using the spin-off https://cs50.stackexchange.com/

Note there is a spin-off site at https://cs50.stackexchange.com/. Consider joining the community and directing your questions there.


The CS50 (a.k.a. CSCI S-50) courses are computer science courses from Harvard University as part of its edX program.

There is a C library available at CS50 Library which provides support functions for the course.

There is also a Python library at CS50 Library which provides support functions for the course.

The tag should be used primarily for questions also tagged or (though the course does use some other languages too, and if there is a need, the extra languages would be valid too).

This tag should be used for questions which use the "cs50.h" header, python-cs50 or -lcs50 library. Another symptom that it is appropriate is the use of the type string (which is defined with typedef char *string; in the cs50.h header) or functions such as get_char(), get_double(), get_float(), get_string(), get_int(), get_long_long() and SQL().

The term 'pset' with a number (e.g. PSET5 — for Problem Set 5), with varying capitalizations is also often an indication that could be appropriate.

  • As an alternative to edx, there is a standalone website (called CS50x) where all the course materials and problem sets can be found.
  • There is CS50 Stack Exchange site dedicated to the course.
  • You can run CS50 programs in Visual Studio Code for CS50 (Codespaces)
  • The previous CS50 IDE has been deprecated. Use the Codespaces link above instead. For reference, here is the link to the previous CS50 IDE
  • CS50 has a "CS50 Sandbox" to run in a test environment. The CS50 Sandbox will be decommissioned on July 1, 2023. Sandbox URL: http://sandbox.cs50.io/ A suggested alternative after that date is Replit
3444 questions
2
votes
2 answers

Error when using two variables in a for statement

for (int a = 0, int x = 1; a < 2; a++, x++) { // Body } When I do this, it shows me an error on that line at the part when I declare x. The error is: error: expected identifier or '(' for (int a = 0, int x = 1; a < 2 && x < 3; a++,…
2
votes
2 answers

check50 finds code incorrect in cs50 course, plurality problem/C

The program should count which candidate get the most votes; and should act like: $ ./plurality Alice Bob Number of voters: 3 Vote: Alice Vote: Charlie Invalid vote. Vote: Alice Alice If there are multiple candidates with a same number of votes…
2
votes
1 answer

What should be the correct way to use 'get_string' function from cs50 library without encountering errors?

When using cs50 library, get_string function doesn't work properly. uppercase.c:6:19: error: too few arguments to function 'get_string' string name = get_string("hey? "); ^~~~~~~~~~ In file included from…
2
votes
1 answer

How do I troubleshoot a segmentation fault(core dumped) error in my cs50 speller code?

So I know it is shameless to ask someone on the internet to debug my code, but I have drained my pea-sized brain to the limit and I still can't solve this (my rubber duck has fled my desk in order to get some peace). All joking aside, I am having…
Gianluca
  • 33
  • 3
2
votes
1 answer

Why is my user defined function repeating twice?

I am programming a piece of code for the credit problem in problem set 1 of CS50. This code already works, but during some debugging I noticed that my first function, which checks whether a credit card number complies with Luhn's algorithm, was…
2
votes
1 answer

Why is the code not working properly? CS50 problem half

I am doing a CS50 problem-Half. I did the problem and it kinda worked, but when entering for example: Bill before tax and tip: 12.50 Sale Tax Percent: 8.875 Tip percent: 20 It outputs $7.80 and not $8.17. When entering: Bill before tax and tip:…
Savana01
  • 23
  • 2
2
votes
1 answer

I want to the same string multiple times in C via printf

I'm doing Harvard CS50, and in the second class on C, the instructor says %s would automatically print each extra argument in succession. Okay, that's cool. But what if I want to print the first string multiple times? #include int…
cssExp
  • 43
  • 5
2
votes
4 answers

Why can't my C function handle punctuation?

I'm trying to do a simple scrabble game in the cs50 pset Week 2 and the function, int compute_score(string word), can't handle an input that uses punctuation, even though it's roughly the same as the correct answer using less lines of code by…
LifeLong21
  • 15
  • 4
2
votes
2 answers

CS50 pset 4 smiley - what's the meaning of code line from license taks?

RGBTRIPLE (*image)[width] = calloc(height, width * sizeof(RGBTRIPLE)) I don't fully understand the code. What I understand is that: calloc(height, width * sizeof(RGBTRIPLE)) - we are organizing a place somewhere in heap memory of a certain size for…
2
votes
2 answers

How to iterate through loop of a string to compare with another string's index?

I am currently facing a problem while coding. The problem is I want to loop through a string and compare each index with another string's index. And at the same time, copy the character to the other string if it does not have it yet. The code below…
ordigam
  • 21
  • 3
2
votes
1 answer

Why is it that when the ball touches the goalie the backdrop(background) does not change to game over background?

Ball Movement Code: Goalie Movement Code: The code was working before, however it is not working now. I tried to rework my code change my blocks for when the ball touches the goalie to send a message to the goalie and the goalie to send a message to…
2
votes
2 answers

How do I concat a character into a string?

I'm trying to take a char and add it to a string. OR if that's not possible, add it to an array of characters? I tried changed 'string updated = ""' to 'char updated[text_length]' but I was getting the same error - incompatible integer to pointer…
matt.mcg
  • 419
  • 5
  • 10
2
votes
1 answer

How to use toupper library function in C?

I need to print the initials of a name, like tyler jae woodbury would print TJW, but I can't seem to print the uppercase initials. This is my code: #include #include #include #include string…
Tyler
  • 21
  • 2
2
votes
1 answer

Undeclared identifier when printing inside a for loop, although it seems the identifier is clearly stated

When printing a variable inside a loop I get an error message about a undeclared identifier. The variable that gives the error is "j". // Record preference if vote is valid bool vote(int voter, int rank, string name) { for (int i = 0; i <…
Neuquert
  • 59
  • 7
2
votes
2 answers

How can I convert chars(pointer) to strings in C - to be used in strcmp

I am trying to create a function that takes a string as an input and compares each character to each other to ensure they don't repeat. The string consists of 26 alpha characters The goal is to compare the 26 characters to each other to ensure that…
Sizwe
  • 51
  • 5