Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C] Scanf function causing bugs

scanf c scanf c scanf bug scanf freeze scanf glitch scanf not working

Best Answer sepp2k, 16 March 2013 - 06:02 AM

What does your input look like? Are you sure you've entered four integers? Note that you have to separate the input by whitespace even if your format string does not contain whitespace. If you enter 1234 without spaces in between, that's one integer - not four.

 

PS: A newline is written as \n, not /n.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 brh8447

brh8447

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 16 March 2013 - 05:32 AM

Hey guys, I'm taking an Intro to C class and for our first program we're doing a simple arithmatic algorithm. I compiled the source code and ran it. After my the inputs are given for my printf function, the scanf function freezes the program. I've read up on it a decent amount and I know that scanf is notorious for being complicated for beginners and that there are better alternatives. Unfortunately we're required to use it for this project. If anyone could take a look and tell me what's up in my formatting that would be awesome! Thank you!

 

*sorry my code isn't a scrolling box, if anyone could PM me and tell me how to do this I'd be more than happy, I know how annoying it will get three posts (or maybe 1) in

int main( )
{

/*Establishing variables to be used*/

    int int1, int2, int3, int4, sum, prod, avg, lrg, lrg2, m2m, nlm;

/*Prompts user for input*/

    printf("Please input four different integers:");

/*Stores User Inputs*/

    scanf("%d%d%d%d",&int1,&int2,&int3,&int4);

/*Defines the value of a variables and displays to user.*/

    sum=int1+int2+int3+int4;

    printf("Sum: %d/n",sum);

    prod=int1*int2*int3*int4;

    printf("Product: %d/n",prod);

    avg=sum/4;

    printf("Average: %d/n",avg);


/* Finds lrg value of integers */
    
    lrg=int1;

        if(int1<int2)
            {lrg=int2;}
        if(int2<int3)
            {lrg=int3;}
        if(int3<int4)
            {lrg=int4;}
            
    printf("largest: %d/n",lrg);


/*Defines lrg2 as smallest possible value by working through possible scenarios*/
    
    lrg2=INT_MIN;

    if(lrg==int1);
    
    {
        if(lrg2<int2)
            {lrg2=int2;}
        if(lrg2<int3)
            {lrg2=int3;}
        if(lrg2<int4)
            {lrg2=int4;}
    };

    
    if(lrg==int2);
    {
        if(lrg2<int1)
            {lrg2=int1;}
        if(lrg2<int3)
            {lrg2=int3;}
        if(lrg2<int4)
            {lrg2=int4;}
    };

    
    if(lrg==int3);
    {
        if(lrg2<int1)
            {lrg2=int1;}
        if(lrg2<int2)
            {lrg2=int2;}
        if(lrg2<int4)
            {lrg2=int4;}
    };

        
    if(lrg==int4);
    {
        if(lrg2<int4)
            {lrg2=int4;}
        if(lrg2<int2)
            {lrg2=int2;}
        if(lrg2<int3)
            {lrg2=int3;}
    };


/*Handles Issue of two equal variables*/
    
    if(lrg==lrg2);
    {
        if(int1==int2)
        {
            if(int3<int4)
                {lrg2=int4;}
            if(int4<int3)
                {lrg2=int3;}
        };

        
        if(int1==int3);
        {
            if(int2<int4)
                {lrg2=int4;}
            if(int4<int2)
                {lrg2=int2;}
        };



        if(int1==int4);
        {
            if(int2<int3)
                {lrg2=int3;}
            if(int3<int2)
                {lrg2=int2;}
        };


                    
        if(int2==int3);
        {
            if(int1<int4)
                {lrg2=int4;}
            if(int4<int1)
                {lrg2=int1;}
        };

                    
        if(int2==int4);
        {
            if(int1<int3)
                {lrg2=int3;}
            if(int3<int1)
                {lrg2=int1;}
        };


        if(int3==int4);
        {
            if(int1<int2)
                {lrg2=int2;}
            if(int2<int1)
                {lrg2=int1;}
        };
    };


/*Output of second lrg integer*/

    printf("Second largest: %d/n",lrg2);


/*Defines and outputs ratio of lrg integer to second lrg integer*/

    m2m=lrg/lrg2;

    printf("lrg / Secnd largest: %d/n",m2m);


/*Confirmation to OS that program ran succesfully*/
    return (EXIT_SUCCESS);

 


 


Edited by Roger, 16 March 2013 - 09:36 AM.
tags


#2 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts

Posted 16 March 2013 - 06:02 AM   Best Answer

What does your input look like? Are you sure you've entered four integers? Note that you have to separate the input by whitespace even if your format string does not contain whitespace. If you enter 1234 without spaces in between, that's one integer - not four.

 

PS: A newline is written as \n, not /n.



#3 brh8447

brh8447

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 16 March 2013 - 09:29 AM

That would be it! Thank you for the help. I'm glad someone could explain this since my professor skipped over the fact.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download