d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Basic C Program Questions Help
Prev12
Add Reply New Topic New Poll
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 30 2013 09:04pm
Quote (Saterial @ Jan 30 2013 06:40pm)
Thanks for the great help guys!

Yeah I know how to prompt for input. And I think that "cin" is for c++ and this class is strictly C. However I think I know how to use that idea in C.

On a side note, for the min/max question; I am not allowed to use arrays. So that would mean I cannot use abducts method I assume?

Awesome help!


You couldn't use it because of the std::cin function but if that were not the case you could use it because he doesn't declare any array just multiple int variables.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jan 30 2013 09:19pm
Quote (SelfTaught @ Jan 30 2013 11:04pm)
You couldn't use it because of the std::cin function but if that were not the case you could use it because he doesn't declare any array just multiple int variables.


you thought he was in a c++ class

oh well logic is still there
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 31 2013 02:34pm
Hey guys! Everything worked out, I just have one more question. My program compiles and works fine, but how do I make it so that you can use Y and N for true and false in loops?

Code
#include <stdio.h>

int main()
{
       int x;                                                                                                            
       int a, b, c, d, e;                                                                                  
       int answer;

       do
{
       printf("Please enter a five digit number: ");                            
       scanf("%d", &x);                              

       a = x / 10000;            
       b = (x / 1000) % 10;                                    
       c = ((x / 100) % 100) % 10;  
       d = (((x / 10) % 1000) % 100) % 10;
       e = x % 10;

       printf("The digits of %d are: ", x);                  
       printf("%d %d %d %d %d\n", a,b,c,d,e);  

       printf("Would you like to try again? 1 to continue or 0 to stop: ");    
       scanf("%d", &answer);                    
}
       while(answer != 0);                            
       printf("Goodbye!\n");                    
       return 0;              
}


So as you can see, I have "Would you like to try again? 1 to continue or 0 to stop." How can I make it "Would you like to try again? Y or N:"

I tried adding:

char answer;
...
scanf("%s", &answer)
while(answer != n);

But obviously that doesn't work. How can I make it work?

This post was edited by Saterial on Jan 31 2013 02:35pm
Member
Posts: 2,478
Joined: Jan 4 2007
Gold: 7,545.00
Jan 31 2013 03:09pm
Shouldn't it be:

Code

while(answer != 'n')


You're missing single quotes which makes the n a char.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 31 2013 06:53pm
Code
#include <stdio.h>

int main()
{
      int x;                                                                                                            
      int a, b, c, d, e;                                                                                  
      char answer;

      do
{
      printf("Please enter a five digit number: ");                            
      scanf("%d", &x);                              

      a = x / 10000;            
      b = (x / 1000) % 10;                                    
      c = ((x / 100) % 100) % 10;  
      d = (((x / 10) % 1000) % 100) % 10;
      e = x % 10;

      printf("The digits of %d are: ", x);                  
      printf("%d %d %d %d %d\n", a,b,c,d,e);  

      printf("Would you like to try again? 1 to continue or 0 to stop: ");    
      scanf("%d", &answer);                    
}
      while(answer != 'n' || answer != 'N');                            
      printf("Goodbye!\n");                    
      return 0;              
}


Code
while(answer != 'n' || answer != 'N');


so that it will stop if the decide to enter an uppercase n
Member
Posts: 194
Joined: Jun 21 2003
Gold: 17.10
Jan 31 2013 07:34pm
I actually tried adding that! But I made an error and I don't know why it was an error, could you explain?

I had while(answer != 'n' || 'N'); why would that give me an error? Isn't it essentially while answer is n or N?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Jan 31 2013 07:51pm
Quote (Saterial @ Jan 31 2013 05:34pm)
I actually tried adding that! But I made an error and I don't know why it was an error, could you explain?

I had while(answer != 'n' || 'N'); why would that give me an error? Isn't it essentially while answer is n or N?


it needs to be

Code
while(answer != 'n' || answer != 'N');


it has to be that way because essentially if it were the way you did it, it would translate to do...while('N'); which doesn't make any sense

This post was edited by SelfTaught on Jan 31 2013 07:53pm
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll