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
5 answers

Struct in C (assign value into array of struct)

I have this two different code writing in C. Both of them just try to assign value into structure. And i'm using cs50 library to help me to get string in simple way. For the first code, when i'm trying to execute it got an error. First…
user14348893
2
votes
1 answer

Can't make Structures to work in C (Using GCC)

So I was just practicing Structures in C and I encountered a big problem... I am attaching the code and the errors The code I was trying to run is to put a predefined value in the structure and then print it. Code #include struct…
Otus9051
  • 67
  • 1
  • 7
2
votes
3 answers

Why the for loop is filling the whole array with the latest string?

Apologies if this is simple, but I am new to C. I am trying to create a loop that fills in an empty array of strings with multiple strings. However, at the end, the whole array is being filled with the latest element ! Below is the code: int…
user9051697
  • 119
  • 8
2
votes
2 answers

Why does this program written in C not print a value of 5 when comparing an input and some arrays?

#include #include #include #include // Points assigned to each letter of the alphabet int POINTS[] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 }; char ALPHABET[] = { 'a'…
JLAc91
  • 35
  • 3
2
votes
2 answers

how to store string and float from a file to a struct?

Im trying to store string and floats from a file to a struct. I have managed to store floats to the struct, but the strings won't work the same. my file looks like this Names weight(kg) John Smith 56.5 Joe …
Mark
  • 21
  • 3
2
votes
3 answers

Not sure how to deal with repeating decimals in C

int number = round(2/3); printf("%i", number); the output is 0 I expected to get 1 as 0.6 recurring should round up - i know for sure that the problem is with recurring decimals as they are always rounded down - I'm not sure how to round up in…
Dev
  • 31
  • 4
2
votes
1 answer

C, toupper segmentation fault

char *s1 = "emma"; char *s2 = s1; s2[0] = toupper(s2[0]); printf("%s\n", s2); printf("%s\n", s1); I am messing and studying with pointers but i don't quite understand why i'm getting a segmentation error here. I know that…
user14512537
2
votes
1 answer

My attempt at Luhn's algorithm (for credit card validity) seems to recognise some cards but not others

I've constructed some code to simulate Luhn's algorithm for checking the validity of credit cards. It successfully recognises American Express cards (15 digit numbers beginning in 34 or 37), but when I try Mastercard (16 digits, beginning with 51,…
BlueKhakis
  • 293
  • 1
  • 8
2
votes
3 answers

Reading command-line argument in C and checking is it a digit

Details about my goal In C, my program is supposed to take an input from command line, read it, make sure that it is a digit and then print out "Success" if true, "Usage: ./caesar key" if false. Expected results ./caesar 33 Success or ./caesar…
Nermin
  • 47
  • 1
  • 8
2
votes
3 answers

Creating a Scrabble like game in C. How to check if a string contains certain chars, then refer the char to an array and score it accordingly

I am doing CS50 and I have an assignment of creating a "Scrabble" like game. Here are the implementation details. The goal is for two players to enter their words and the higher scoring player wins. Points are scored in an array called…
Nermin
  • 47
  • 1
  • 8
2
votes
1 answer

ERROR: incompatible integer to pointer conversion assigning to 'string' (aka 'char *') from 'int'

I created an array with 26 letters and I want to assign each elements to their ASCII values like that: ASCII[0] = 65 ASCII[1] = 66 I'd already defined ASCII[0] as "A" in my array. So the result that I hope should be: A = 65 B = 66 So what is the…
2
votes
3 answers

Do you have any simple hash function that takes first three letter?

I'm working at cs50 speller. This is my hash function. It works well. But I think it's not really good because there's a lot of if condition. I try to make a different table for every word that have apostrophe at the first two letter (ex, A', B',…
user14348893
2
votes
1 answer

Why is my python Mario program less comfortable ignoring the spaces?

I'm working on cs50 pset6 python Mario, and it's supposed to take an int as input (the int is named height) and then make a mario pyramid of size(height), like this: $ python mario.py Height: 4 # ## ### #### But my program prints this: ~/ $…
Lost in code
  • 313
  • 1
  • 4
  • 17
2
votes
2 answers

CS50 - Compiling Issues

I am currently taking the cs50 Harvard course via EDX and am working on the Cash/Greedy Algorithm problem from PSET1/Week1. I have it all written out and I keep getting this error; ~/pset1/ $ clang -o cash cash.c -lcs50 /tmp/cash-4f9816.o: In…
Josh Gray
  • 69
  • 1
  • 1
  • 5
2
votes
3 answers

"*** stack smashing detected ***" with file read and writes

I'm working with a C program that reads from a single file and uses sprintf to write that data into multiple files, I'm going wrong somewhere, but I don't really know where and that results in this error: *** stack smashing detected *** The code…