Reply to topic  [ 15 posts ] 
Where's my C gone horribly worng? 
Author Message
Doesn't have much of a life
User avatar

Joined: Fri Apr 24, 2009 4:00 pm
Posts: 940
Location: Pompy
Reply with quote
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:
Code:
#include <stdio.h>

int main( void )
{
    char a, b, c, d;
    scanf( "%c", &a, &b, &c, &d );   
    printf( "%d", a + b + c + d );
}   


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:
Quote:
Please enter more characters.
103


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
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Is it looking for an input of "1 2 3 4"?


Fri Oct 22, 2010 2:39 pm
Profile WWW
Doesn't have much of a life
User avatar

Joined: Fri Apr 24, 2009 4:00 pm
Posts: 940
Location: Pompy
Reply with quote
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
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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...

Code:
scanf("%c%c%c%c", &a, &b, &c, &d)


for the first bit of code?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri Oct 22, 2010 3:02 pm
Profile WWW
Doesn't have much of a life
User avatar

Joined: Fri Apr 24, 2009 4:00 pm
Posts: 940
Location: Pompy
Reply with quote
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
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
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

_________________
Twitter
Charlie 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
Profile
Doesn't have much of a life
User avatar

Joined: Fri Apr 24, 2009 4:00 pm
Posts: 940
Location: Pompy
Reply with quote
finlay666 wrote:
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

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
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
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

_________________
Twitter
Charlie 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
Profile
Doesn't have much of a life
User avatar

Joined: Fri Apr 24, 2009 4:00 pm
Posts: 940
Location: Pompy
Reply with quote
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
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
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>
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define EXITSUCCESS (0);
#define EXITTOOFEW (1);
#define EXITTOOMANY (2);

int main(int argc, char** argv)
{
   int c,n, count = 0;
   int nums[4];
   
   while((c = getchar()) != EOF)
   {
      n = 0;
      if(isdigit(c))
      {
         do
         {
            n *= 10;
            n += atoi(&c);
            c = getchar();
         } while(isdigit(c));

         nums[count] = n;
         count++;
      }
      
      if(count >4)
      {
         printf("Too many input characters\n");
         printf("Please enter 4 digits.\n\n");
         return EXITTOOMANY;
      }
   }

   if(count < 4)
   {
      printf("Too few input characters\n");
      return EXITTOOFEW;
   }
   else if(count == 4)
   {
      /*Do whatever*/
      int i;
      printf("Success\n");
      for(i = 0; i < 4; ++i)
      {
         printf("Number %d: %d\n", i, nums[i]);
      }
   }

   return EXITSUCCESS;
}

<eddit>Another problem is blanks and non-numeric inputs. You'll need to strip them.<eddit>Added</eddit></eddit>

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Last edited by EddArmitage on Sun Oct 24, 2010 10:03 pm, edited 1 time in total.



Fri Oct 22, 2010 7:50 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
mars-bar-man wrote:
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.

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

_________________
Twitter
Charlie 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
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
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?

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Sun Oct 24, 2010 8:06 pm
Profile
Doesn't have much of a life
User avatar

Joined: Fri Apr 24, 2009 4:00 pm
Posts: 940
Location: Pompy
Reply with quote
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
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
Cool, glad to hear it. Just because I hate leaving incomplete code on the interwebz, I've updated my code above.

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Sun Oct 24, 2010 10:00 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 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

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.