View unanswered posts | View active topics
It is currently Mon Jun 02, 2025 2:38 am
|
Page 1 of 1
|
[ 15 posts ] |
|
Where's my C gone horribly worng?
Author |
Message |
mars-bar-man
Doesn't have much of a life
Joined: Fri Apr 24, 2009 4:00 pm Posts: 940 Location: Pompy
|

Okay, I have a little problem. I need to create a C program that when 4 inputs are put in, say "abcd" the out displays the ASCII values added together. Here's the bear minimum code I need which works: It compiles but that's it, not much more tbh. Now, what I want to do is create an if/else statement so that if 5 inputs are plugged in it'll throw up an error saying "Too many inputs" and vice versa for too few inputs. Here's what I have so far:  |  |  |  | Code: #include <stdio.h>
int main( void ) { char a = NULL, b = NULL, c = NULL, d = NULL, e = NULL; scanf( "%c%c%c%c%c", &a, &b, &c, &d, &e ); if( e = NULL ) { printf( "Too many characters entered. \n" ); } else if( a = NULL ) { printf( "%c \n" ); } else( b = NULL ); { printf( "Please enter more characters. \n" ); } printf( "%d", a + b + c + d ); } |  |  |  |  |
And if I were to input "1234" it gives this output: I'm pretty sure the ASCII total is wrong as well. Any pointers on what I'm doing wrong here? I've sort of cobbled this all together from memory, lecturer hasn't put his lecture slides up containing the if/else function. This is due in on the 24th, I've got the bare minimum to pass this piece, I'd just like to do a little better than that. Thanks. Seb
_________________Just your friendly neighbourhood mars-bar-man.flickr
|
Fri Oct 22, 2010 2:34 pm |
|
 |
forquare1
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 6:36 pm Posts: 5150 Location: /dev/tty0
|
Is it looking for an input of "1 2 3 4"?
|
Fri Oct 22, 2010 2:39 pm |
|
 |
mars-bar-man
Doesn't have much of a life
Joined: Fri Apr 24, 2009 4:00 pm Posts: 940 Location: Pompy
|
No, it's just 4 characters, I can input anything I want as long as it's 4 characters long, I haven't tried any punctuation marks yet, but numbers work as well as the whole alphabet.
_________________Just your friendly neighbourhood mars-bar-man.flickr
|
Fri Oct 22, 2010 2:43 pm |
|
 |
Fogmeister
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 7:35 pm Posts: 6580 Location: Getting there
|
Does the first bit of code work OK? i.e. compiles, runs, etc... If so, what is the output for entering "abcd"? I would have thought the scanf line should be... for the first bit of code?
|
Fri Oct 22, 2010 3:02 pm |
|
 |
mars-bar-man
Doesn't have much of a life
Joined: Fri Apr 24, 2009 4:00 pm Posts: 940 Location: Pompy
|
First bit of code compiles and runs fine, and I get an output of 394. And if I add the extra 3 %c it doesn't make any difference.
I've placed them in the larger code because I remember my lecturer mentioning it, but alas I can't confirm because he hasn't uploaded his lecture slides yet.
_________________Just your friendly neighbourhood mars-bar-man.flickr
|
Fri Oct 22, 2010 3:34 pm |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
Can you read the data in in a different format? given that you need to strip out whitespace etc
Looks like you aren't reading in enough characters to me
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Fri Oct 22, 2010 4:03 pm |
|
 |
mars-bar-man
Doesn't have much of a life
Joined: Fri Apr 24, 2009 4:00 pm Posts: 940 Location: Pompy
|
Okay, I'm a total n00b with C atm, I've been doing it for 4 weeks, and well, didn't pick it up all that quickly. Care to explain in a more.. simple way?
_________________Just your friendly neighbourhood mars-bar-man.flickr
|
Fri Oct 22, 2010 4:11 pm |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
well you read in each character, so a character can be your numbers, or even a space
a number (in this case) is read as a number of characters so '1234' is '1''2''3''4' and '1 2 3 4' is '1'' ''2'' '3'' ''4' so reading in 5 characters on "1 2 3 4" only gives you '1'' ''2'' '3'
I haven't used /C++ in a while though but will try and look tonight if I am feeling up to it as I'm a bit worse for wear atm
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Fri Oct 22, 2010 4:26 pm |
|
 |
mars-bar-man
Doesn't have much of a life
Joined: Fri Apr 24, 2009 4:00 pm Posts: 940 Location: Pompy
|
Thanks Finn, I'm using bog standard normal C though.
I know this can work because a mate who was sat next to me in the lab session managed to get it to work, and we both have similar code. Just would like the extra points so if it all falls apart on the next piece it won't be so bad.
_________________Just your friendly neighbourhood mars-bar-man.flickr
|
Fri Oct 22, 2010 4:34 pm |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
Couple of ways to approach this. One would be to read in char by char using getchar(void) or similar, or you could modify what you have.[/quote] As Fin said, I'd look at doing something scanning char by char. Here's something I've literally hacked as fast as I can type, and I'd want validation on the I/O, but as a starter: <eddit>Now pretty complete</eddit> <eddit>Another problem is blanks and non-numeric inputs. You'll need to strip them.<eddit>Added</eddit></eddit>
Last edited by EddArmitage on Sun Oct 24, 2010 10:03 pm, edited 1 time in total.
|
Fri Oct 22, 2010 7:50 pm |
|
 |
finlay666
Spends far too much time on here
Joined: Thu Apr 23, 2009 9:40 pm Posts: 4876 Location: Newcastle
|
C++ is just C with some extra features to make it more object oriented and adding support for strings over the fixed length char arrays (aka C-String) so the methods are mostly the same
_________________TwitterCharlie Brooker: Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.
|
Fri Oct 22, 2010 9:08 pm |
|
 |
forquare1
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 6:36 pm Posts: 5150 Location: /dev/tty0
|
Is it reading the new line character? If you only input three characters "123" then hit return, is that OK?
|
Sun Oct 24, 2010 11:49 am |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
How goes it, mars_bar_man? I was bored on a train earlier today so finished a char-by-char implementation if you're interested?
|
Sun Oct 24, 2010 8:06 pm |
|
 |
mars-bar-man
Doesn't have much of a life
Joined: Fri Apr 24, 2009 4:00 pm Posts: 940 Location: Pompy
|
It's all good, thanks. Cheers for all the help! I've decided not to go too far ahead with it though, to barely pass the code has to compile, to get a higher pass, I need more comments and then higher again, more detailed ones. To get max points I need to go beyond that with the if/else functions, in which lies a problem, if I can't perfectly explain the function I'll lose points, which sucks. So I've done what I know is correct and I'm happy with that, hopefully our next piece of coursework will explore if/else functions a bit more! Once again, thanks! 
_________________Just your friendly neighbourhood mars-bar-man.flickr
|
Sun Oct 24, 2010 9:27 pm |
|
 |
EddArmitage
I haven't seen my friends in so long
Joined: Thu Apr 23, 2009 9:40 pm Posts: 5288 Location: ln -s /London ~
|
Cool, glad to hear it. Just because I hate leaving incomplete code on the interwebz, I've updated my code above.
|
Sun Oct 24, 2010 10:00 pm |
|
|
|
Page 1 of 1
|
[ 15 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 8 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum
|
|