C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 31st, 2013, 02:10 PM
lovecodecakes lovecodecakes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 14 lovecodecakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 47 sec
Reputation Power: 0
While loop skipping

Hi I have a very simple problem in the c code:

The while loop is completely being skipped!

Code:
int main()



 



{



 



int dial=0;



 



int temp=0;



 



printf(“Enter the total no. of elements \n”);



 



scanf(“%d”,&temp);



 



printf(“%d \n”,temp); //to check



 



char arr[temp];



 



if((arr[dial])!=”NULL”)



 



{



 



while(arr[dial]!=”NULL”)



 



{



 



printf(“Enter the N elements one by one, with element# %d: \t”,dial+1);



 



scanf(“%c”,&arr[dial]);



 



dial++;



 



printf(“\n”);



 



}



 



}



 



}

Reply With Quote
  #2  
Old January 31st, 2013, 02:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,127 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 18 h 27 m
Reputation Power: 1949
Crappiest ... code listing ... ever!

Code:
int main()
{
    int dial=0;
    int temp=0;

    printf(“Enter the total no. of elements \n”);
    scanf(“%d”,&temp);
    printf(“%d \n”,temp); //to check

    char arr[temp];

    if((arr[dial])!=”NULL”)
    {
        while(arr[dial]!=”NULL”)
        {
            printf(“Enter the N elements one by one, with element# %d: \t”,dial+1);
            scanf(“%c”,&arr[dial]);
            dial++;
            printf(“\n”);
        }
    }
}


Now look at these lines:
Code:
    if((arr[dial])!=”NULL”)
    {
        while(arr[dial]!=”NULL”)

What are you trying to do? arr is a single C-style string. Since you have not initialized it, it contains garbage, which is to say whatever effectively random values just happened to have been left there. Never attempt to use uninitialized variables!

Also arr[dial] would be a character, a single character. So then why are you trying to compare it with the address of a string literal? What do you think that you are trying to accompish? If you are going to compare a single character to anything, then it needs to a another single character!

What did the compiler's warnings tell you? Don't tell me there weren't any warnings! The compiler had to squack and complain very loudly at the nonsense that you were feeding it! Don't tell me you were stupid enough to turn the warnings off. Or even more stupid enough to have ignored the warnings. Never ever ignore warnings! And never run a program that hasn't compiled cleanly -- meaning without any warnings. Because when a compiler issues warnings, it's because it's confused by the grave mistakes you made and, in its confusion, will guess at what you intended, which will almost never be what you did intend, and so generate code that will not do what you wanted it to do! Never ignore warnings! They are much more important than error messages!

Even if you had attempted something like
if (arr != "NULL")
with the intention of testing whether arr contained a string that was different than "NULL", that would not do what you thought it did. All you would be doing would be to compare memory addresses, testing whether the literal "NULL" was located in the same place in memory as the arr array, which they are not. That means that you would never ever be able to leave that while loop, which is an infinite loop problem, not your stated "it ignored the while". Though since you are testing a character against the address of a string, who knows what the crazy code generated by your poor compiler could actually be doing?

Your code is terminally messed up. Turn warnings on, read the warnings, and make the corrections needed.

PS
And the final statement in your main function needs to return an int. Try return 0;, which indicates that the program had run successfully and did not encounter any run-time errors.

Reply With Quote
  #3  
Old February 1st, 2013, 02:57 PM
ProgramGuruCpp ProgramGuruCpp is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 7 ProgramGuruCpp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 33 m 28 sec
Reputation Power: 0
Thumbs up

What compiler are you using, may I ask? I would think that most compilers wouldn't even let you compile.

One of your problems is that you used "NULL" (a string literal) instead of NULL (a constant). Some compilers support this by default while others may not.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > While loop skipping

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap