GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Mar-2004, 01:00
Homestar Homestar is offline
New Member
 
Join Date: Mar 2004
Posts: 9
Homestar is on a distinguished road

Simple error that i cant find... irritating me


ok when q is used to quit it prints out the intialized values of the rest of the array to 0... i dont want it to print these... also when the tenth integer is entered and the program ends the last integer entered is not printed but is initialized to 0 instead... any suggestions?.. im just trying to familiariz myself witht this stuff.. and this is being difficult.

CPP / C++ / C Code:
#include <stdio.h>
int x[10];
void main()
{
    int i;
    printf("Enter a list of up to 10 integers, q to quit.\n" );
    for (i = 0; i <= 9; i++)
    {
   
    scanf("%d\n", &x[i]);
    	
    }
    printf("Array in reverse order:\n " );
    for (i = 9; i >= 0; i--)
    {
    printf("%5d", x[i]);
    }
    printf("\n");

}
  #2  
Old 30-Mar-2004, 03:46
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I'm not seeing where you have "q to quit" implemented in your code.
__________________
-Aaron
  #3  
Old 30-Mar-2004, 08:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Don't know about 'q' for quit, but heres a problem:

Change
CPP / C++ / C Code:
    scanf("%d\n", &x[i]);

to
CPP / C++ / C Code:
    scanf("%d", &x[i]);

When you have problems like this, I suggest you put a printf() after the place where you obtain user input (to make sure the program sees what you think it out to see).

For example

CPP / C++ / C Code:
scanf("%d", &x[i]);
printf("You entered: %d\n", x[i]);

" printf() is your friend."


Regards,

Dave
  #4  
Old 30-Mar-2004, 11:14
Homestar Homestar is offline
New Member
 
Join Date: Mar 2004
Posts: 9
Homestar is on a distinguished road
Quote:
Originally Posted by aaroncohn
I'm not seeing where you have "q to quit" implemented in your code.
There is no code for q to quit because none is needed.... anything besides an int value entered for scanf is not recognized and it stops.
  #5  
Old 30-Mar-2004, 11:15
Homestar Homestar is offline
New Member
 
Join Date: Mar 2004
Posts: 9
Homestar is on a distinguished road
Quote:
Originally Posted by davekw7x
Don't know about 'q' for quit, but heres a problem:

Change
CPP / C++ / C Code:
    scanf("%d\n", &x[i]);

to
CPP / C++ / C Code:
    scanf("%d", &x[i]);

When you have problems like this, I suggest you put a printf() after the place where you obtain user input (to make sure the program sees what you think it out to see).

For example

CPP / C++ / C Code:
scanf("%d", &x[i]);
printf("You entered: %d\n", x[i]);

" printf() is your friend."


Regards,

Dave
Ok but the problem is when im printing out everything in the end... i get all the user inputs fine but i just dont want the arrays that were not used to print out zero... how do i tell it only to print the arrays used...
  #6  
Old 30-Mar-2004, 12:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Homestar
Ok but the problem is when im printing out everything in the end... i get all the user inputs fine but i just dont want the arrays that were not used to print out zero... how do i tell it only to print the arrays used...

The program that you posted requires 10 entries then prints out 10 values. Your scanf() is expecting an integer. If the user enters anything that is not an integer, it goes back through the loop for a total of 10 times. Each time scanf() is still looking at the (non-integer) character in the input buffer, so doesn't store anything in the array. I guess that global variables are initialized to 0, so you see zeros in places for which you haven't given values.

This shows why lots of people say that scanf() is not very good for user input.

Dave
Last edited by davekw7x : 30-Mar-2004 at 13:27.
  #7  
Old 30-Mar-2004, 13:27
Homestar Homestar is offline
New Member
 
Join Date: Mar 2004
Posts: 9
Homestar is on a distinguished road
Quote:
Originally Posted by davekw7x
The program that you posted requires 10 entries then prints out 10 values. Your scanf() is expecting an integer. If the user enters anything that is not an integer, the goes back through the loop for a total of 10 times. Each time scanf() is still looking at the (non-integer) character in the input buffer, so doesn't store anything in the array. I guess that global variables are initialized to 0, so you see zeros in places for which you haven't given values.

This shows why lots of people say that scanf() is not very good for user input.

Dave
I figured it out... needed a while statement and stuff but i got it. Well if not using scanf() then what is better to use?.. any advice is helpful at my nub stage in this.
  #8  
Old 30-Mar-2004, 13:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Homestar
I figured it out... needed a while statement and stuff but i got it. Well if not using scanf() then what is better to use?.. any advice is helpful at my nub stage in this.

Lots of people use fgets() to get a complete line into a buffer, then sscanf(), atoi() or lots of other possibilities (including roll-your-own tests) for extracting numbers, characters, etc.

Dave
  #9  
Old 30-Mar-2004, 17:28
Homestar Homestar is offline
New Member
 
Join Date: Mar 2004
Posts: 9
Homestar is on a distinguished road
Quote:
Originally Posted by davekw7x
Lots of people use fgets() to get a complete line into a buffer, then sscanf(), atoi() or lots of other possibilities (including roll-your-own tests) for extracting numbers, characters, etc.

Dave
o ok thanx man
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Where can I find web statistics script / software? rhino1616 Web Design Forum 2 02-Jan-2004 21:31
HELP!: find window width? Center a <div>? Kingherc Web Design Forum 2 06-Jul-2003 06:25
How Do i get php to find out the file type of a file for me? viperman95833 MySQL / PHP Forum 2 08-Mar-2003 10:48
How do I find the Unix log file route? jrobbio Web Hosting Forum 4 04-Feb-2003 08:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.