Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here i have a looping code that keeps on looping if it encounters a 1, I want to be able to loop the program with either the user entering a 1 or 'A'

What I have tried:

<pre> #include <stdio.h>
 int main()
 {
    int choice = 1;
   while (choice == 1){
   int n, count =0;
   printf("enter an integer = ");
   scanf("%d", &n);
   int buffer = n;
   while (buffer!=0)
   {
     buffer/=10;
     count++;
   }
   printf("your number %d has %d digits", n, count);
   printf("\n Do you want to use another number? (1 or A if Yes 2 Or B if NO)\n");
   scanf("%d", &choice);
 }
 return 0;
 } 


Now with this exact code I can only loop if I enter the value 1, How can I also loop with giving the user the ability to enter either a 1 or 'A'
Posted
Updated 7-Jan-19 0:16am
Comments
CPallini 7-Jan-19 6:13am    
Following Patrice's suggestion, test on character values ('1', 'A').

Keyboard input is always a char
C++
scanf("%d", &choice);

In this scan, you ask to convert to numeric.
choice must be a char
1 is an integer, '1' is a char

Advice: use the debugger to see wgat is going on in your code.
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 7-Jan-19 6:11am    
5.
Patrice T 7-Jan-19 6:23am    
Thank you
First off, indent your code! If you don't - and you haven't - then it gets harder to work out what is going on. It's not too bad with small code like this, but when it gets bigger it can get extremely difficult if you don't indent correctly. Indentation style - Wikipedia[^]
I don't care if you use Whitesmiths:
#include <stdio.h>
int main()
   {
   int choice = 1;
   while (choice == 1)
      {
      int n, count =0;
      printf("enter an integer = ");
      scanf("%d", &n);
      int buffer = n;
      while (buffer!=0)
         {
         buffer/=10;
         count++;
         }
      printf("your number %d has %d digits", n, count);
      printf("\n Do you want to use another number? (1 or A if Yes 2 Or B if NO)\n");
      scanf("%d", &choice);
      }
   return 0;
   }
Or even the execrable 1TB:
#include <stdio.h>
int main(){
   int choice = 1;
   while (choice == 1){
      int n, count =0;
      printf("enter an integer = ");
      scanf("%d", &n);
      int buffer = n;
      while (buffer!=0){
         buffer/=10;
         count++;
      }
      printf("your number %d has %d digits", n, count);
      printf("\n Do you want to use another number? (1 or A if Yes 2 Or B if NO)\n");
      scanf("%d", &choice);
   }
   return 0;
}
Just pick a style and stick to it!

When you use scanf to read from the console, the type of value it returns depends on exactly what you specify in the format string: in your case "%d" fetches an integer value, so if your user types "1" and presses ENTER it will work ... but if he presses "A" it will lock up, crash, or otherwise fail because "A" is not an integer number!

What I would suggest is that you use scanf to return a string, and parse that to find out what the user entered:
char inp[100];

scanf("%s", inp);
choice = atoi(inp);
And get rid of the "A" / "B" option.
 
Share this answer
 
Comments
Stefan_Lang 17-Jan-19 8:26am    
Never heard of '1TB' as indentation style, only memory size ;-p
I know that style under the name 'egyptian brackets' (see https://blog.codinghorror.com/new-programming-jargon/ ;-) )

Personally I think Whitesmiths is the worst possible style as it's the style where it's most difficult to spot the end of a block. This is extra nasty in concatenated if-else blocks.

Any other style is pretty much equivalent with regard to readability, with one exception: when an expression (loop or if condition) before the opening bracket gets too long, or requires multiple line, I don't like appending the opening bracket at the end (of the last line). Instead I prefer putting it on the next line, even when using egyptian style otherwise. That said, it's probably better to stick to the former (opening bracket on new line), always.

IMHO
OriginalGriff 17-Jan-19 8:40am    
"One True Bracket" - an abomination in the sight of all that is development.
Conversely, I find Whitesmiths the most readable, as all the code that is related to a block is indented to the same level, regardless of whether it is a single line of code, or a block of code with brackets.

But it takes all sorts to make a world!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900