473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to prevent the previous result from showing

I'm 10th grade doing our project in IT.

It tasked us to convert decimal to binary and ask the user if he wants to try again or not.

I did that using the goto but when I type another decimal it shows the previous result

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. int n, r, i=1, b=0; 
  4. char command;
  5. int main(void)
  6.  
  7. {
  8. start:
  9.     printf("enter a decimal: ");
  10.     scanf(" %i", &n);
  11.  
  12.     while(n>0)
  13.  
  14.     { 
  15.     r=n%2;
  16.     n=n/2;
  17.     b=b+r*i;
  18.     i=i*10;
  19.  
  20.     }
  21.     printf("%i", b);
  22.  
  23.  
  24.  char command;
  25.     printf("\n Would you like to try again?[Y/N] \n");
  26.  
  27.     scanf(" %c", &command);    
  28.  
  29.     while(command=='Y')
  30.     {
  31.         goto start;
  32.         break;
  33. }
  34.  
  35.     printf("Goodbye!");
  36.  
  37.  
  38.  
  39.     return 0;  
  40.     }
Sep 17 '17 #1
2 2051
weaknessforcats
9,208 Expert Mod 8TB
I didn't do a complete debug but I did notice your variable i is not initialized when you start your loop. Therefore, i has the value from the first number when the loop starts the second number. The effect is to add the binary digits of the second number behind those of the first number.

Now when you display i as a char you see the bits from the first number.

I would display either define i as a char or leave it an int and display it as an int.

BTW you ask the user for a decimal but you are expecting an int. I would ask for an integer.
Sep 17 '17 #2
It's quite simple, you need b to start at 0, and i at 1 everytime.
When you restart from the goto, it keeps their current values, so you should put b=0; i=1; straight after the start :
Sep 20 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: george | last post by:
Can anyone help? I query a database and return a result on the column "reference". There might be 7 listings. Each row is displayed in a table, with links through to a detail page. I am working...
22
by: alecjames1 | last post by:
I have a form which the user must complete before closing. I have disabled the window x button and use my own exit button. When selected it checks to see if the user has completed the entries...
14
by: beginner10 | last post by:
How can i changhe this code showing how many persons i saved to the file? And it prints pretty much rubbish. Where is the problem? #include <stdio.h> int main() { int i; FILE *data_file;...
3
by: Brian Henry | last post by:
Is there anyway to prevent the white box that drops down when you do a combobox dropdown from showing? I am trying to make a custom combobox which requires showing a custom form in its place, but...
11
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. ...
3
by: Ben | last post by:
Hello I am in the final stages of developing my asp.net app and have a question. The app im creating has two frames, one being a menu and the other showing the detail. I would like to prevent...
9
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
I'm working on a litigation web app where users will review images of case documents. One of the requirements is that we either prevent the images from being cached on the clients machine (in temp...
2
by: Matsam | last post by:
Hi, I have an ASP page that displays the account details of individual users, to which they can login using their user name & password. I have given a link for "Logout". When a user clicks this...
3
by: Marc Brown | last post by:
I keep on getting the same error message and I don't know how to either solve the problem or prevent it from showing the error. I have set up a switchboard item that opens a report. When the report...
2
by: Shepard | last post by:
Hi guys, I was validating a field emp. when condition is true it should display an alert and go to home page.. This is my code.. if (emp== "" || emp==null) { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.