473,413 Members | 2,051 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,413 software developers and data experts.

URGENT - how to make 0 to stop?

I have one C programming problem:

Enter value, 0 to stop:
15
26
45
0

List now : 15 26 45 0 37814140
Value to change : 26
Want to change with value: 45
List now: 15 45 26 0 37814140

The problem is when I key in 0, it should stop, and the list only can show up to 45 only.
Why my output show 0 and 37814140, izzit my code is worng?

Below is my code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define max 5
  3. int main()
  4. {
  5.     int num[max], i=0;
  6.     int swapFromNum = 0;
  7.     int swapToNum = 0;
  8.     int swapFromArrIndex = 0;
  9.     int swapToArrIndex = 0;
  10.     bool swapFromNumFound = false, swapToNumFound = false;
  11.  
  12.     printf("Enter value , 0 to stop: \n");
  13.     while(1)
  14.       {
  15.        scanf("%d", &num[i]);   // read value
  16.        if(num[i]==0)
  17.        break;       // if 0 finished
  18.        i++;
  19.       }
  20.  
  21.     printf("List now: ");
  22.  
  23.       for(i=0;i<max;i++)
  24.        {
  25.         printf("%d ",num[i]);
  26.        }
  27.        printf("\n");
  28.  
  29.  
  30.      printf("Value to change:");
  31.      scanf("%d",&swapFromNum);
and some more....
Mar 8 '07 #1
2 1589
Ganon11
3,652 Expert 2GB
Suppose the user enters 3 values, then 0. (35, 20, 14, 0). Then there are only 4 values in the array - the 5th element is garbage, correct?

But when you display the list, you loop i from 0 to max (which is 5), printing every element of the array, garbage or not. You should keep track of how many values the user entered before the 0.
Mar 8 '07 #2
AdrianH
1,251 Expert 1GB
Suppose the user enters 3 values, then 0. (35, 20, 14, 0). Then there are only 4 values in the array - the 5th element is garbage, correct?

But when you display the list, you loop i from 0 to max (which is 5), printing every element of the array, garbage or not. You should keep track of how many values the user entered before the 0.
And you might notice that if you enter 10 numbers, strange things may happen after 5 and you may crash your programme.


Adrian
Mar 8 '07 #3

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

Similar topics

3
by: Daniel Ng | last post by:
Thanks for the 2 guys how hep to to solve to count the number of recount in forum. I'm done wif that. Got 1 more question.. Example Record 1: ID 2 VALUE 2 Record 2: ID 3 VALUE 3 Record 3: ID 2...
2
by: Edwinah63 | last post by:
Hi Everyone, All the very best for 2004!! i need urgent help with this problem, the users are about to skin me alive!! we have an access front end with linked to sql server 2k tables. ...
1
by: Amit D.Shinde | last post by:
Hi Experts, i am writting a stored procedure in sql server 7. Its a simple stored procedure It is my first stored procedure. I want insert a record in table if the primary key field user id...
0
by: Amratash | last post by:
Hi, I needyour help.Its urgent. My main aim is to log the site activities in a database at runtime. I'm using W3C Extended Log File format for logging information. I can use ODBC Logging directly...
2
by: blazerguns | last post by:
Hi all, I have used WMI method to set ipaddress in my system. But the problem is that if i have multiple iface cards this program is setting all the nic to same ip. I dont know how to stop this...
4
by: davidw | last post by:
I have xml tree like <root> <field name="l1"> <field name="l11"> <field name="l111"/> <field name="l112"/> </field> <field name="l2" stop="true"> <field name="l21">
1
by: Honey | last post by:
the following is the programming code,..i need to stop the rebound animation on a click of the mouse and continue its animation on another click so far I could stop the animation by using...
10
by: Teja | last post by:
HI all, I have a problem in accesing COM objects in threads. To be precise, lets assume that I have a class GenericFunctions which is defined as follows: import win32com.client, pythoncom,...
1
by: kommaraju | last post by:
iam a starter to db2 & jdbc.i have a servlet program which connects to ibm db2 using jdbc.when i run this using apache tomcat 4.1.34 , it is showing a error message of HTTP STATUS 500 my jdbc...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.