9 Replies - 473 Views - Last Post: 04 December 2010 - 05:21 AM Rate Topic: -----

#1 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Looping Array of Numbers

Posted 04 December 2010 - 02:35 AM

How can i fix this? I want the user to input 5 numbers and then print it out. The problem was after i input 5 numbers it display nothing how can i fix it?

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

void main(void)
{
	char Numbers[5];
	int i = 0, x;
	clrscr();

	while(i <= 4)

	{
		printf("Enter a number: ");
		scanf("%d", &x);
		Numbers[i] = x;
		i++;
	}

	while(Numbers[i] != '\0')
	{
		cout << Numbers[i] << endl;
		i++;
	}
	getch();

}



Is This A Good Question/Topic? 0
  • +

Replies To: Looping Array of Numbers

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: Looping Array of Numbers

Posted 04 December 2010 - 02:40 AM

After you've gathered all the values, you have to reset the value of i to zero.
Was This Post Helpful? 1
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Looping Array of Numbers

Posted 04 December 2010 - 02:43 AM

what is this about?
while(Numbers[i] != '\0')



Where is your thinking at?

You can't just recycle ideas from old programs you didn't understand.
You do need to think about what is happening in this program and what you want to do.
Was This Post Helpful? 1
  • +
  • -

#4 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Looping Array of Numbers

Posted 04 December 2010 - 02:49 AM

but look at this... after i made an easier program, it display unknown numbers..

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

void main(void)
{
	char Numbers[5];
	Numbers[0] = 5;
	Numbers[1] = 4;
	Numbers[2] = 3;
	Numbers[3] = 2;
	Numbers[4] = 1;
	int i = 0;
	clrscr();

	while(Numbers[i] != '\0')
	{
		printf("%d\n", &Numbers[i]);
		i++;
	}
	getch();

}



it displays - numbers

pls i dont know how... we are already in file handling and now that i have to use this kind of problem i cant remember or i dont know rather .. :(

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

void main(void)
{
	char Numbers[5];
	Numbers[0] = 5;
	Numbers[1] = 4;
	Numbers[2] = 3;
	Numbers[3] = 2;
	Numbers[4] = 1;
	int i = 0;
	clrscr();

	while(i != 5)
	{
		printf("%d\n", &Numbers[i]);
		i++;
	}
	getch();

}


im trying but it cant help it ...

This post has been edited by issyl: 04 December 2010 - 02:45 AM

Was This Post Helpful? 0
  • +
  • -

#5 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Looping Array of Numbers

Posted 04 December 2010 - 03:02 AM

nopencil may i know how you convert this to c++?

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

void main(void)
{
	char Numbers[5];
	Numbers[0] = 5;
	Numbers[1] = 4;
	Numbers[2] = 3;
	Numbers[3] = 2;
	Numbers[4] = 1;
	int i = 0;
	clrscr();

	for(i = 0; i <= 4; i++)
	{
/* I failed on this Numbers[i] nevermind my last post im so stupid &&&&&&  :(/> */
		cout << Numbers[i] << endl;
		//printf("%d\n", Numbers[i]);
	}


	getch();
}


Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,341
  • Joined: 20-August 07

Re: Looping Array of Numbers

Posted 04 December 2010 - 03:06 AM

View Postissyl, on 04 December 2010 - 09:49 AM, said:

	char Numbers[5];
	Numbers[0] = 5;
	Numbers[1] = 4;
	Numbers[2] = 3;
	Numbers[3] = 2;
	Numbers[4] = 1;
	int i = 0;
	clrscr();

	while(i != 5)
	{
		printf("%d\n", &Numbers[i]);


im trying but it cant help it ...
The problem is in the way you're using printf. Do you understand what the & operator does? If not look it up in your book. Also read up on the usage of the printf function if you are not sure how to pass arguments, and the exact meaning of format specifiers.

As someone else has already mentioned in this thread - you need to understand the code you are writing, because you will never get anywhere if all you're doing is throwing random symbols in and hoping for the best.

This post has been edited by Bench: 04 December 2010 - 03:08 AM

Was This Post Helpful? 1
  • +
  • -

#7 issyl  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 118
  • Joined: 25-October 10

Re: Looping Array of Numbers

Posted 04 December 2010 - 03:31 AM

can anyone convert this line of code:

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

void main(void)
{
	char Numbers[5];
	Numbers[0] = 5;
	Numbers[1] = 4;
	Numbers[2] = 3;
	Numbers[3] = 2;
	Numbers[4] = 1;
	int i = 0;
	clrscr();

	for(i = 0; i <= 4; i++)
	{
/* THIS COUT << NUMBERS[I] << ENDL; DONT WORK... HOW CAN I FIX THIS ONE?
THE ORIGINAL IS printf("%d\n", Numbers[i]); BUT TO CONVERT TO C++ HOW? ANYONE? */
		cout << Numbers[i] << endl;
		//printf("%d\n", Numbers[i]);
	}

/*
	for ( day = 1; day <= DAYS_IN_WEEK; ++day)
		printf("Sales for day %hi = %5hi\n", day, sales[day - 1]);
  */
	getch();
}


Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1708
  • View blog
  • Posts: 3,283
  • Joined: 30-May 10

Re: Looping Array of Numbers

Posted 04 December 2010 - 04:30 AM

It IS C++.
What you need to do it tell us something more informative than "it doesn't work".

But then again, you're using #include <iostream.h>, which has been obsolete for over a decade now, and you're using void main which has never been valid. So who knows what kind of fossil compiler you're using.
Was This Post Helpful? 1
  • +
  • -

#9 budfox  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 03-December 10

Re: Looping Array of Numbers

Posted 04 December 2010 - 04:45 AM

If you read ints you must define ints (and not chars).
Remove the non portable conio stuff from your programs and don't mix C++ and C codeparts.
#include <stdio.h>

#define NNUM 5
int main()
{
	int Numbers[NNUM];
	int i = 0;

	while(i < NNUM)
	{
		printf("Enter a number: ");
		scanf("%d", &Numbers[i++]);
	}

        i=0;
	while(i < NNUM)
	{
          printf("%d\n",Numbers[i++]);
	}
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Looping Array of Numbers

Posted 04 December 2010 - 05:21 AM

issyl, you have GOT to stop opening up duplicate topics. It's not going to get you help for your copy & paste code endeavors any more quickly; it's only serving to annoy me and others.

Merged duplicate topics.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1