How to fix "expected identifier or '(' in C compilation? - cs50

I am new to coding and I keep getting stuck in the first few lines of code and I cannot figure out why. This is what I have so far:
#include <stdio.h>
#include <cs50.h>
int main(void);
int n;
{
printf("Minute: ");
int n = get_int();
}
I am getting this message when I try to compile the code:
What did I do wrong?

You're trying to call the main function. You should only define it. It will be called when the program is executed (it is the "entry point").
To define it, remove the semicolon after
int main(void)
You can also remove that void keyword
Then move that line down, between
int n; and the { that comes after it
Additionally, you're declaring the n variable twice. After you fix the first error, the compiler will complain about this one. Remove one of the declarations then.

You should remove the semicolon after int main(void) and move the variable declaration for n within the braces. Here is the correct code below.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int n;
printf("Minute: ");
int n = get_int();
}

Related

Wrong output with scanf function

so this is supposedly not a difficult question, but I've been getting this problem a few times when running my code in VS code. I am trying to separate the alphabets and numbers from the string, and I have used the method as follows (in my code) according to what is taught in the book. However, despite having the program running, the output is wrong.
Here is my code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height = 0;
char wunit[] = "";
char hunit[] = "";
printf("Enter the body weight: ");
scanf("%d%s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%s",&height,hunit);
printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
The thing is,if I type in 20lb for weight, and 30mt for height, what happens is that it gives the output: 20,t,30,mt; which generates this weird ‘t’ instead of lb, and I have no idea why this is the case.
Similarly, when I type 30kg for weight, and 20cm for height. It generates this weird output:30,m,0, cm. The kg becomes a 'm' and the 20 is now a '0'!? Why is that the case? The expected output would be 30,kg,20,cm
I tried simply replacing the strings, but that doesn't solve the problem fundamentally. For instance, (considering when my user puts logical inputs like lb or kg for weight), I tried this substitution and it appears to work, but doesn't fix the issue of making 20 -> 0
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height= 0;
char wunit[] = "";
char hunit[] = "";
char wunit2[] = "lb";
char dummy[] = "t";
printf("Enter the body weight: ");
scanf("%d%s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%s",&height,hunit);
if (strcmp(wunit,dummy)==0){
printf("%d,%s,%d,%s\n", weight, wunit2, height, hunit);
}
//printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
I've also tried running it in codecollab, and it shows this error of "stack smashing detected" after I run it a few times, which got me more confused, what has it to do with this?
Thanks in advance.
wunit is an array of size 1 (it is initialized to "", which in chars looks like {'\0'}). What happens when you try to put lots of characters (say, "lb", which is {'l', 'b', '\0'}) into a memory location that is smaller than it should be?
scanf happily writes as many bytes as needed, smashing anything in its way ("stack-smashing", because wunit and all those local variables are stored on the stack). Try to give scanf more space, say using
char wunit[10] = "";
And never ever use "%s" directly. Limit the maximum of characters that you will allow scanf to place, for example using "%9s" to ensure that at most 9 characters + terminator (10 total) will be read.
This works for me:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int weight = 0;
int height = 0;
char wunit[10] = "";
char hunit[10] = "";
printf("Enter the body weight: ");
scanf("%d%9s",&weight,wunit);
printf("Enter the height: ");
scanf("%d%9s",&height,hunit);
printf("%d,%s,%d,%s", weight, wunit, height, hunit);
return 0;
}
Note: scanf with %s is rightfully considered very dangerous. See https://stackoverflow.com/a/2430310/15472

Simple Cypher Program Not Working (CS50)

On week 2 of CS50 and I've hit a wall. My code is supposed to prompt a user for plaintext and then print a simple cypher on the next line. Problem is, my code keeps printing the exact input for the user rather than scrambling. My code is below.
Note: the error in my code is likely down in the for loop, inside the respective printf functions.
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (int argc, string argv[]){
if (argc != 2){
printf("You must enter two arguments, the second being a single digit integer!\n");
return 1;
}
int key = atoi(argv[1]);
printf("What do you want to encrpyt?");
string s = get_string();
for(int i=0; i < strlen(s); i++){
if (isupper(s[i])==true){
printf("%c",((s[i] + key)));
}
if (islower(s[i])==true){
printf("%c",s[i] + key);
}
else {
printf("%c",s[i]);
}
}
}
Fixed it. The if statement syntax was wrong, so the program was skipping over the cypher text. I need to delete the "==true" out of the if statement.

Can't use memcpy on char * attribute as destination - visual c++

I have the following piece of code in appPOSWebDlg.cpp:
#include "stdafx.h"
#include "afx.h"
#include <stdlib.h>
#include <Ras.h>
...
//Attribute
char *site;
...
// Method
int readFile() {
char * aux;
int result;
result = readParameter(hFile, aux);
if (result == 0) {
memcpy(site, aux, 256);
} else {
return 1;
}
return 0;
}
But the program stops at the memcpy line and I'm not sure why. After debugging, I can confirm that the aux parameter is being assigned correctly with the value expected. Furthermore, I even used the memcpy inside the readParameter method to assign it and had no problem. So why can't I assign that value to attribute site using the same method?
Your "site" pointer is invalid. You've defined it as a pointer, but, not allocated any space for it, so, your copy command is overlaying some code. You'll need to allocated the pointer correctly by performing a "new" and a "delete" when you are done.

Combining a static char* and a constant string

I want to be able to append a constant string to the end of another string in the form of a char*, and then use the resulting string as an argument for open(). Here's what it looks like:
file1.cpp
#include "string.h"
file2 foo;
char* word = "some";
foo.firstWord = word; //I want the file2 class to be able to see "some"
file2.h
#include <fstream>
#include <iostream>
#define SECONDWORD "file.txt"
class file2{
public:
file2();
static char* firstWord;
static char* fullWord;
private:
ofstream stream;
}
file2.cpp
#include "file2.h"
char* file2::firstWord;
char* file2::fullWord;
fullWord = firstWord + SECONDWORD; //so fullWord is now "somefile.txt" ,I know this doesn't work, but basically I am trying to figure out this part
file2::file2(){
stream.open(fullWord);
}
So I am not very well versed in C++, so any help would be appreciated!
C++-style solution might be the following.
#include <string>
char* a = "file";
char* b = ".txt";
...
stream.open((std::string(a) + b).c_str());
What happens here? First, std::string(a) creates a temporary std::string object. Than b value is added to it. At last, c_str() method returns a c-style string which contains a + b.

Error initializing XGCValues

I was following this tutorial, bit stuck here:
This code doesn't compile, and the error message is
c:35: error: invalid initializer
I'm not sure what's wrong with the line
XGCValues valu=CapButt|JoinBevel;
infact, I copied it from the said tutorial. Here's the full code I have:
#include <stdio.h>
#include <X11/Xlib.h>
#include <unistd.h>
int main()
{
Display *display=XOpenDisplay(NULL);
int scr=DefaultScreen(display);
Window root_window=RootWindow(display,scr);
unsigned int width=DisplayWidth(display,scr)/3;
unsigned int height=DisplayHeight(display,scr)/3;
unsigned int border=2;
Window my_win=XCreateSimpleWindow(display,root_window,0,0,width,height,border,BlackPixel(display,scr),WhitePixel(display,scr));
GC gc;
XGCValues valu=CapButt|JoinBevel;
unsigned long valmask=GCCapStyle|GCJoinStyle;
gc=XCreateGC(display,my_win,valmask,&valu);
XDrawLine(display,my_win,gc,5,5,20,20);
XMapWindow(display,my_win);
XFlush(display);
sleep(10);
return 0;
}
Thank You
The example in the tutorial is wrong - if you look in <X11/Xlib.h> or read the XCreateGC man page you'll see XGCValues is a struct, not a integral type, so you would need to initialize it with something like:
XGCValues values;
values.cap_style = CapButt;
values.join_style = JoinBevel;

Resources