x404.co.uk
http://www.x404.co.uk/forum/

Where's my C gone horribly worng?
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=11116
Page 1 of 1

Author:  mars-bar-man [ Fri Oct 22, 2010 2:34 pm ]
Post subject:  Where's my C gone horribly worng?

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

Author:  forquare1 [ Fri Oct 22, 2010 2:39 pm ]
Post subject:  Re: Where's my C gone horribly worng?

Is it looking for an input of "1 2 3 4"?

Author:  mars-bar-man [ Fri Oct 22, 2010 2:43 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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.

Author:  Fogmeister [ Fri Oct 22, 2010 3:02 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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?

Author:  mars-bar-man [ Fri Oct 22, 2010 3:34 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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.

Author:  finlay666 [ Fri Oct 22, 2010 4:03 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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

Author:  mars-bar-man [ Fri Oct 22, 2010 4:11 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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?

Author:  finlay666 [ Fri Oct 22, 2010 4:26 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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

Author:  mars-bar-man [ Fri Oct 22, 2010 4:34 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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.

Author:  EddArmitage [ Fri Oct 22, 2010 7:50 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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>

Author:  finlay666 [ Fri Oct 22, 2010 9:08 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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

Author:  forquare1 [ Sun Oct 24, 2010 11:49 am ]
Post subject:  Re: Where's my C gone horribly worng?

Is it reading the new line character? If you only input three characters "123" then hit return, is that OK?

Author:  EddArmitage [ Sun Oct 24, 2010 8:06 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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?

Author:  mars-bar-man [ Sun Oct 24, 2010 9:27 pm ]
Post subject:  Re: Where's my C gone horribly worng?

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! :)

Author:  EddArmitage [ Sun Oct 24, 2010 10:00 pm ]
Post subject:  Re: Where's my C gone horribly worng?

Cool, glad to hear it. Just because I hate leaving incomplete code on the interwebz, I've updated my code above.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/