Author Topic: Where did I go wrong? Beginner C programmer.  (Read 1084 times)

0 Members and 1 Guest are viewing this topic.

Offline Mr. NinjaTopic starter

  • Jr. Member
  • **
  • Posts: 35
  • Helpful? 0
Where did I go wrong? Beginner C programmer.
« on: July 14, 2012, 11:28:43 AM »
So, basically, I'm starting to learn C from the ground up. I found a good tutorial and I'm learning it on the computer environment first, and then moving to robotics.

I designed this program to see if you were "old". Just for a learning perspective.
Code: [Select]
#include <stdio.h>

int main()
{
    char name;
    int age;
    int end;

    printf( "What is your name?  ");
    scanf( "%c", &name );
    printf( "Hello %c", name);
    printf( "How old are you?  ");
    scanf( "%d", &age );
    do {
    if ( age <= 5 ) {
    printf( "How old are you really?  " );
    scanf( "%c", age );
    }
    else if ( age < 18 ) {
    printf( "You're still a minor, nah-nah neh-nah-nah" );
    end = 1;
    }
    else if ( age < 25 ) {
    printf( "You're a young adult now!" );
    end = 1;
    }
    else if ( age < 35 ) {
    printf( "You're not quite middle aged." );
    end = 1;
    }
    else if ( age < 50 )
    printf( "Be glad you're not old.");
    end = 1;
    }

    else {
    printf( "You're old! Nah-nah neh-nah-nah!"); //this is where it "went wrong"
    end = 1
    }
} while ( end != 1)

    return 0;
}

And then, when I try and build it, I get this message in the build log:

Code: [Select]
C:\Users\David\Desktop\Personal\Poof\Library\nameAge.c||In function 'main':|
C:\Users\David\Desktop\Personal\Poof\Library\nameAge.c|36|error: expected 'while' before 'else'|
C:\Users\David\Desktop\Personal\Poof\Library\nameAge.c|40|error: expected identifier or '(' before 'while'|
C:\Users\David\Desktop\Personal\Poof\Library\nameAge.c|43|error: expected identifier or '(' before '}' token|
||=== Build finished: 3 errors, 0 warnings ===|

I don't know where I went wrong, could someone help?

P.S. I am using Code::Blocks as a compiler.

Offline rmore

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: Where did I go wrong? Beginner C programmer.
« Reply #1 on: July 14, 2012, 12:08:28 PM »
Mr Ninja,

First of all check your curly brackets -  check if the opening brackets are same in number as your closing brackets.

This should take care of a lot of your errors.

I am also new in C programming, and by browsing thru your code I think thats one of the errors there. There could be some thing else that I missed....but first of all check your brackets.

Rmore

Offline Mr. NinjaTopic starter

  • Jr. Member
  • **
  • Posts: 35
  • Helpful? 0
Re: Where did I go wrong? Beginner C programmer.
« Reply #2 on: July 14, 2012, 01:22:56 PM »
Well, thanks. I fixed my original problem, but now, it doesn't work properly.
 When I type in my name, it only appears as the first letter. I'm guessing it has something to do with the variable specification.

And it, for some unknown reason, decided to skip the second scanf command, and won't work. It asks the question then answers immediately with no actual qeury.

New code:
Code: [Select]
#include <stdio.h>

int main()
{
    char name;
    int age;
    int end;

    printf( "What is your name?  ");
    scanf( "%c", &name );
    printf( "Hello %c\n", name);
    printf( "How old are you?  ");
    scanf( "%d", &age );
    getchar();
    do {
    if ( age <= 5 ) {
    printf( "How old are you really?  " );
    scanf( "%d", age );
    }
    else if ( age < 18 && age > 5 ) {
    printf( "You're still a minor, nah-nah neh-nah-nah" );
    end = 1;
    }
    else if ( age < 25 && age >= 18 ) {
    printf( "You're a young adult now!" );
    end = 1;
    }
    else if ( age < 35 && age >= 25 ) {
    printf( "You're not quite middle aged." );
    end = 1;
    }
    else if ( age < 50 && age >= 35 ) {
    printf( "Be glad you're not old.");
    end = 1;
    }

    else {
    printf( "You're old! Nah-nah neh-nah-nah!");
    end = 1;
    }
} while ( end != 1);

    return 0;
}

« Last Edit: July 14, 2012, 01:29:08 PM by Mr. Ninja »

Offline agold

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 1
Re: Where did I go wrong? Beginner C programmer.
« Reply #3 on: July 14, 2012, 06:08:01 PM »
Hi,

Your problem is that you are reading a char (single character) instead of a whole string.
name is defined as a char, if you change that to a char array (char[MAX_NAME_LENGTH]) where MAX_NAME_LENGTH is the longest name you want to be able to read in.

The other change you will have to make is change the scanf line to 'scanf("%s", name);' this reads in a string rather than a char. Note the missing '&', it isn't needed when scanning in strings as name is already an address due to how C does arrays.

Adrian

Offline beachboy612

  • Jr. Member
  • **
  • Posts: 27
  • Helpful? 0
Re: Where did I go wrong? Beginner C programmer.
« Reply #4 on: July 14, 2012, 11:27:20 PM »
I don't understand what you wanted him to replace char with in his code.  Can you elaborate?  I too am a newbie in learning C prgramming

Offline beachboy612

  • Jr. Member
  • **
  • Posts: 27
  • Helpful? 0
Re: Where did I go wrong? Beginner C programmer.
« Reply #5 on: July 14, 2012, 11:58:47 PM »
I was able to display the whole name and get it to give the right command.  However, my software times out after that.
« Last Edit: July 15, 2012, 12:06:06 AM by beachboy612 »

Offline beachboy612

  • Jr. Member
  • **
  • Posts: 27
  • Helpful? 0
Re: Where did I go wrong? Beginner C programmer.
« Reply #6 on: July 14, 2012, 11:59:52 PM »

Code: [Select]

#include <stdio.h>


int main()
{
    char name;
    int age;
    int end;

    printf( "What is your name?  ");
    scanf("%s", &name);
    printf( "Hello %s\n", &name);
    printf( "How old are you?  ");
    scanf( "%d", &age );
    getchar();
    do {
    if ( age <= 5 ) {
    printf( "How old are you really?  " );
    scanf( "%d", age );
    }
    else if ( age < 18 && age > 5 ) {
    printf( "You're still a minor, nah-nah neh-nah-nah" );
    end = 1;
    }
    else if ( age < 25 && age >= 18 ) {
    printf( "You're a young adult now!" );
    end = 1;
    }
    else if ( age < 35 && age >= 25 ) {
    printf( "You're not quite middle aged." );
    end = 1;
    }
    else if ( age < 50 && age >= 35 ) {
    printf( "Be glad you're not old.");
    end = 1;
    }

    else {
    printf( "You're old! Nah-nah neh-nah-nah!");
    end = 1;
    }
} while ( end != 1);

    return 0;
}
« Last Edit: July 15, 2012, 12:06:24 AM by beachboy612 »

Offline agold

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 1
Re: Where did I go wrong? Beginner C programmer.
« Reply #7 on: July 15, 2012, 01:03:06 AM »
Hi,

I'll try to explain it a bit better, sorry that wasn't very descriptive before.

If what I say here doesn't make sense I'd recommend a C string tutorial which may explain it better than me.

In C, a 'string' is actually an array of characters. For example, the string "hello world" is stored as {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '\0'} with each letter being a different element of the array. It is null terminated which means that the null character ('\0') is used to signify the end of the string. So while "hello world" has 11 characters in total it takes up 12 slots in an array.

So if we define a char array like so:
Code: [Select]
char myString[15] = "hello world";The first 11 elements of the array are the letters, the 12th is a '\0' (NULL) and the rest can be anything, they won't be displayed typically.

So to give an example with some code, here we go (note, if you're not running via cmd/terminal you might want a 'scanf("%s", name);' at the end to stop the screen disappearing immediately).

Code: [Select]
#include <stdio.h>
int main()
{
    char name[15]; //max name length = 14 (plus NULL terminator = 15)
    printf("Enter your name: ");
    scanf("%s", name);
    printf("Your name is: %s", name);
}

Hopefully that's a bit clearer!

If you don't understand part of it let me know,

Adrian

Offline Mr. NinjaTopic starter

  • Jr. Member
  • **
  • Posts: 35
  • Helpful? 0
Re: Where did I go wrong? Beginner C programmer.
« Reply #8 on: July 22, 2012, 03:17:55 PM »
Thanks, I got it working now!

Now off to learn more C.

(sorry for the late reply, I've been really busy lately)

 


Get Your Ad Here