Thread: Need help in a fairly simple C prog

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    Need help in a fairly simple C prog

    Having a little trouble with a basic program that i wrote. it's supposed to convert the digital number input into a text output. when i compile the program it shows no errors but when i run it it has an error that causes it to close. I'm trying to teach myself c prog right now but there's always questions that arise. Thanks

    Code:
    #include <stdio.h>
    
    int main()
    {
    int num1;
    float num1;
    
    printf("Please enter a single digit number:");
    scanf("%f",num1);
    
    if(num1==1)
    printf("%s %f","You entered the number one",num1);
    
    else if(num1==2)
    printf("%s %f","You entered the number two",num1);
    
    else if(num1==3)
    printf("%s %f","You entered the number three",num1);
    
    else if(num1==4)
    printf("%s %f","You entered the number four",num1);
    
    else if(num1==5)
    printf("%s %f","You entered the number five",num1);
    
    else if(num1==6)
    printf("%s %f","You entered the number six",num1);
    
    else if(num1==7)
    printf("%s %f","You entered the number seven",num1);
    
    else if(num1==8)
    printf("%s %f","You entered the number eight",num1);
    
    else if(num1==9)
    printf("%s %f","You entered the number nine",num1);
    
    else
    printf("error");
    
    scanf("%f", num1");
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    Code:
    int num1;
    float num1;
    you cant declare the same variable for two data types....
    remove one of them...
    Code:
    printf("&#37;s %f","You entered the number one",num1);
    the syntax of your printf is wrong....

    when u want to print a constant string like "hello" you don't need a %s


    so your correct statement would be...

    Code:
    printf("you entered the number one %f",num1);
    
    the output would be...
    
    you entered the number one 1
    try using a switch case instead of elseif ladder....

    why are you using scanf before return 0.... kindly explain...
    you can as well remove that

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    2
    I fixed all the things you mentioned. I noticed that i declared num1 twice after i posted.
    I ran the program and still got the error that causes the program to crash.

    I've had problems with having my programs stay open after they perform their function. I was told before that if i throw a random scanf at the bottom then the program would be looking for a value and pretty much hold until if found that value.

    The reason i chose to use the elseif ladder is because the book that im reading through gives only 2 examples of switch so im not so clear on that. i'll give it a shot

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    Lisbon, Portugal
    Posts
    19
    Code:
    printf("Please enter a single digit number:");
    scanf("%f",&num1);
    That's one error that popped right up, You must save the scanf to the adress, not the variable itself.

    Also, you're much better off using Switch, Let me jsut write a small example.

    Code:
    int main(void){
    int op;
    
    printf("Insert a Number > ");
    scanf("%d",&op);
    
    switch(op)
    {
       case 1: printf("The Number you've inserted is one"); break;
       case 2: printf("The number you've inserted is two"); break;
        . . . 
       case 9: printf("The Number you've inserted is nine"); break;
       default: printf("Wrong Number introdution");
    }
    return 0;
    }
    "Case" lets you put all the expected cases you have, in this case, I should be form 0 to 9. And default, is if the program reads anythign different form that cases, so, if you type 10, it will not detect an option, and returns that error message!

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    57
    One other thing, I think the scanf call is wrong. Need a & in there(pointers - there fun and scary and your probably still work your way up to them).

    Heres a version with a switch in it. Pretty simple.

    If your on windows, you can add system("PAUSE") to the end of you program instead of the scanf. Or you could just start it from a console/shell(aka cmd).

    Code:
    #include <stdio.h>
    
    int main() {
    	int num1;
    	
    	printf("Please enter a single digit number:");
    	scanf("%i", &num1);
    	
    	switch(num1) {
    		case 1:
    			printf("You entered the number one %i", num1);
    			break;
    		case 2:
    			printf("You entered the number two %i", num1);
    			break;
    		case 3:
    			printf("You entered the number three %i", num1);
    			break;
    		case 4:
    			printf("You entered the number four %i", num1);
    			break;
    		case 5:
    			printf("You entered the number five %i", num1);
    			break;
    		case 6:
    			printf("You entered the number size %i", num1);
    			break;
    		case 7:
    			printf("You entered the number seven %i", num1);
    			break;
    		case 8:
    			printf("You entered the number eight %i", num1);
    			break;
    		case 9:
    			printf("You entered the number nine %i", num1);
    			break;
    		default:
    			printf("error");
    	}
    	
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Help on simple text console prog...
    By Azh321 in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 07:03 AM
  5. unable to get simple program working
    By toom in forum Linux Programming
    Replies: 1
    Last Post: 10-11-2003, 05:05 AM