09-4-2013, 08:30 PM | #1 |
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,117
|
Coding in C: help with if statements
Hey peeps! Just started getting into C for University. I am experimenting with the language, and so far I came with a little problem. To begin with, here's my code:
Code:
#include <stdio.h> int main (void) { /*Declarations --------------------------------*/ char yourname[30]; /*Program--------------------------------------*/ printf("What's your name, bro? "); scanf("%s", yourname); // I ask the user to write his name if (yourname == "Felix") // If the name entered is Felix, he gets a warm welcome. { printf("Hello master %s, king of the universe.\n", yourname); } else // If the name entered is anything other than Felix, the program doesn't recognize you. { printf("I don't know you\n"); } } Weirdly enough, when I try to make a similar program using numbers this time around (integers, to be exact), it works like a charm. This is what makes me wonder why it doesn't work when I type letters. Here's an example: Code:
#include <stdio.h> int main (void) { /*Declarations --------------------------------*/ int yourage; /*Program--------------------------------------*/ printf("What's your age, bro? "); scanf("%d", &yourage); // I ask the user to write his age, this time. if (yourage == 20) // If the name entered is 20 years old, he gets a warm welcome. { printf("Hello my master, king of the universe.\n"); } else // If the age is anything other than 20, the program doesn't recognize you. { printf("I don't know you\n"); } } |
09-4-2013, 08:39 PM | #2 |
FFR Simfile Author
|
Re: Coding in C: help with if statements
Do you know how strings (really an array of characters in C) work in C? Are you familiar with the concept of pointers?
In any case, using strcmp() when comparing strings will likely solve your issue. |
09-4-2013, 08:41 PM | #3 |
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,117
|
Re: Coding in C: help with if statements
Sadly, I am not. I thought strings would work just like numbers. Really, why is there a difference?
|
09-4-2013, 08:50 PM | #4 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,586
|
Re: Coding in C: help with if statements
Code:
#include <stdio.h> #include <string.h> int main() { char yourname[30]; scanf("%s", yourname); // http://www.cplusplus.com/reference/cstring/strcmp/ if(strcmp(yourname, "Felix") == 0) { printf("Hello master %s, king of the universe.\n", yourname); } else { printf("I don't know you..."); } while(1); return 0; } Do you know what will happen if you type in more than 30 characters? Edit: I'm glad to see you've adopted the open brace style, instead of the Egyptian style. (Stick with it!) I would also like to highly recommend against using CTRL + F5 to run your code, always use only F5. You should also get into the habbit of hitting Ctrl + Shift + B to compile before running. Of course, if you are using gcc and vim, this wont mean anything. This is under the assumption that you are using Visual Studio. Last edited by Zageron; 09-4-2013 at 08:58 PM.. |
09-4-2013, 08:54 PM | #5 |
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,117
|
Re: Coding in C: help with if statements
Are you using C++? I can't even compile this code, I get the following errors:
Cstring no such files or directory 'new' undeclared (it thinks it's a variable) Implicit declaration of strcmp 'delete' undeclared And I don't know what happens if I type more than 30 characters, it doesn't seem to cut them. It won't compile if I don't specify a number though... I'm a real beginner btw rofl. Thanks for the help though, really appreciate it! |
09-4-2013, 08:56 PM | #6 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,586
|
Re: Coding in C: help with if statements
Haha, my bad. You're using C. I definitely missed that, what a derp I am.
I'll simplify the code so it doesn't confuse the **** out of you. (And also convert it into C) Edit: Changed the code. Sorry, I'm a C/C++ fusion programmer. |
09-4-2013, 09:08 PM | #7 |
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,117
|
Re: Coding in C: help with if statements
Hey, thanks a whole bunch. I'll keep this thread as a reference, I had no idea about the strcmp command. On the other hand, just to end this topic, I was wondering about why you included these commands:
while(1); return 0; When I tried to remove them to see if they were essential, I saw no noticeable change. What do they do? |
09-4-2013, 09:13 PM | #8 | |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,586
|
Re: Coding in C: help with if statements
Quote:
The return 0 is good practice. If you are returning a type, you should always return a value from that type from all code paths. Example: Code:
int main() { if(true) { return 0; } else { return 1; } } Last edited by Zageron; 09-4-2013 at 09:17 PM.. |
|
09-4-2013, 10:31 PM | #9 |
no
Join Date: Jan 2004
Age: 33
Posts: 1,850
|
Re: Coding in C: help with if statements
this is an infinite loop because booleans that aren't equal to 0 always evaluate to true in C/C++. you won't ever be able to gracefully exit your program with this here. i would recommend against doing this and instead use http://www.cplusplus.com/reference/cstdio/fgets/ (keep in mind this is unsafe; more robust methods of halting are used in production code) so you can control when your program exits.
this isn't doing what you think it is doing. you aren't comparing an array of characters to a string literal, you are comparing the address of the beginning of an array to a string literal. this isn't going to make much sense until you find out how arrays are actually implemented, but use strcmp as was posted here. because you don't always want the contents of the array concatenated together into a string -- sometimes you just want the location of the array in memory. this is something else that probably won't make sense until you discuss pointers. Last edited by Fission; 09-4-2013 at 10:42 PM.. |
09-4-2013, 11:18 PM | #10 | ||||
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: Coding in C: help with if statements
Quote:
http://en.wikipedia.org/wiki/Null-terminated_string Here's the pertinent information from that link: Quote:
Quote:
Does anyone know where to find comprehensive documentation or help guides? We can post Wikipedia links all day, but that doesn't compare to something like the Python documentation. |
||||
09-4-2013, 11:29 PM | #11 |
no
Join Date: Jan 2004
Age: 33
Posts: 1,850
|
Re: Coding in C: help with if statements
two references i have used in the past:
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ http://en.cppreference.com/w/c the latter is a bit incomplete, but has still proven to be helpful where other references have failed. |
09-13-2013, 07:10 PM | #12 |
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,117
|
Re: Coding in C: help with if statements
Hey y'all! Back with another problem. I'm now trying to integrate a specific function which should supposedly give back the value close to pi=3.1415... and compare this value to the actual value of pi.
Here is a normal code I made which works right: This is fine and all. Now, I am trying to do the same thing, but for different N's, in other words, different fractions of the x=0 to the x=1 line. This is the one that crashes: Code:
#include <stdlib.h> #include <math.h> #include <plplot.h> #define I 8 /* I will be the maximal value of the j's linked to each N. We must not got further than this value. */ int main(void) { /* Declarations ------------------------------------------*/ double x[i]; double xi, xo; /* The ends of our interval */ int k, j; double f[i]; /* This function will become f(x)=(1-x^2)^(1/2) */ double sum; double error; int N[i]; double pi; pi=M_PI; /* pi is really equel to pi */ /* Executable --------------------------------------------*/ xi=0; xo=1; N[1]=3; /* I manually assignate a value for each N */ N[2]=10; N[3]=30; N[4]=100; N[5]=300; N[6]=1000; N[7]=3000; N[8]=10000; j=0; while (j<I) /* I do a loop of the precedent code for each different value of N already pre-defined */ { j++; sum = 0; /* Accumulation variable */ for (k=0; k<N[j]; k++) { x[k]=xi+k*(xo-xi)/(N[j]-1); /* Divide the line going from x=0 to x=1 in N-1 increments and go an increment farther each time this loop restarts */ f[k]=sqrt(1-pow(x[k],2)); /* The function on which we integrate */ sum+=2*(f[k]+f[k-1])*(x[k]-x[k-1]); /* Area of each trapeze that we sum with the accumulation variable named 'sum' */ } error=fabs(pi-sum); printf("For N=%d, Error= fabs( %f - %f ) = %f\n", N[j], sum, pi, error); } /* printf("sum = %2.10f\n", sum); */ printf("\nEnd of line.\n\n"); } Last edited by MarioNintendo; 09-13-2013 at 07:13 PM.. |
09-13-2013, 08:44 PM | #13 |
FFR Veteran
Join Date: Nov 2006
Posts: 382
|
Re: Coding in C: help with if statements
Umm, in the second code segment what is the value of i?
I don't see where it is initialized
__________________
|
09-14-2013, 11:02 AM | #14 |
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,117
|
Re: Coding in C: help with if statements
Oops, my bad, it should read "I" instead of "i". Still, that doesn't seem to be the reason why it crashes, because when I manually change the N[j] values to smaller numbers, everything works fine.
|
09-14-2013, 11:43 AM | #15 |
FFR Veteran
Join Date: Nov 2006
Posts: 382
|
Re: Coding in C: help with if statements
Change "x[i] " and "f[i]" to "x[M]" and "f[M]". Then, add the line "#define M 10000" to the top of your program. Let me know if that fixes your issues.
What's happening is that you have only allocated space for 8 doubles in your arrays. In your for loop, the variable 'k' can range between 0 and 9999. So, when you execute the following lines: x[k]=xi+k*(xo-xi)/(N[j]-1); f[k]=sqrt(1-pow(x[k],2)); you are writing to memory that was not allocated for those arrays. Also, arrays are zero indexed. So in the following lines: N[1]=3; /* I manually assignate a value for each N */ N[2]=10; N[3]=30; N[4]=100; N[5]=300; N[6]=1000; N[7]=3000; N[8]=10000; you should start counting from 0 and not 1. The element N[8] is actually the 9th element and is not in the space you have allocated for the array.
__________________
Last edited by Shouka; 09-14-2013 at 12:05 PM.. |
09-14-2013, 12:29 PM | #16 | ||
Expect delays.
Join Date: Mar 2008
Location: Montreal, QC
Age: 31
Posts: 4,117
|
Re: Coding in C: help with if statements
Quote:
Quote:
thank you soooooooo much. Here's the final code: Last edited by MarioNintendo; 09-14-2013 at 03:16 PM.. |
||
09-14-2013, 11:51 PM | #17 |
no
Join Date: Jan 2004
Age: 33
Posts: 1,850
|
Re: Coding in C: help with if statements
why not just use a for loop? also, the general convention is to use "<" for counting loops, so you would just use j < 8 to accomplish the same thing.
Last edited by Fission; 09-15-2013 at 12:00 AM.. |
09-15-2013, 02:36 AM | #18 | |
new hand moves = dab
Join Date: Dec 2002
Location: he/they
Age: 33
Posts: 10,094
|
Re: Coding in C: help with if statements
Quote:
In particular, you ran into trouble with these lines. Code:
for (k=0; k<N[j]; k++) { x[k]=xi+k*(xo-xi)/(N[j]-1); f[k]=sqrt(1-pow(x[k],2)); sum+=2*(f[k]+f[k-1])*(x[k]-x[k-1]); } Shouka's suggestion fixed the problem because, by changing that line with #define in it, you also changed the sizes of your arrays. Your x and f arrays now have enough spots for x[k] and f[k] to be valid for all values which will be assigned to k in your program. This line defines an identifier, i, as holding a value of 100000. i is not a variable; you cannot change its value after this line. I wanna say these identifiers are conventionally in all caps and are spelled out to describe what the value represents. So for this one, just to help make the code more readable, you could use something like Code:
#define MAX_NUM_ITERATIONS 10000 Last edited by dAnceguy117; 09-15-2013 at 02:40 AM.. |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | |
|
|