The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
user input to 2d array
Discuss user input to 2d array in the C Programming forum on Dev Shed. user input to 2d array C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 6th, 2004, 03:56 AM
|
 |
Contributing User
|
|
Join Date: Sep 2003
Location: New Zealand
|
|
|
user input to 2d array
Hi all I hope someone can help.
im trying to take a users inputs from dos such as product and prices and put them into a 2d array then output that array of prices and products after they have finished entering say 10 of them..
very confusing and I cant seem to get it to work at the moment, if anyone has some tips that won't go to far over my head that would be great.
cheers
Dirk
|

January 6th, 2004, 10:13 AM
|
 |
Contributing User
|
|
Join Date: Dec 2003
Location: London, ON, Canada, eh
Posts: 127
Time spent in forums: 11 h 11 m 42 sec
Reputation Power: 10
|
|
|
Unless the product is a float you used use a struct.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char product[100];
float price;
} Input;
int main (int argc, char *argv[])
{
int i;
int NUMBER_OF_INPUTS = 3;
Input input[NUMBER_OF_INPUTS];
float price;
for (i=0; i < NUMBER_OF_INPUTS; ++i)
{
fprintf (stdout, "Enter a product: ");
fscanf (stdin, "%s", input[i].product);
fflush(stdin);
fprintf (stdout, "Enter a price: ");
fscanf (stdin, "%f", &input[i].price);
fflush(stdin);
}
for (i=0; i<NUMBER_OF_INPUTS; ++i)
{
fprintf (stdout, "%s:$%.2f\n", input[i].product,input[i].price);
}
return 0;
}
|

January 6th, 2004, 06:14 PM
|
 |
Contributing User
|
|
Join Date: Sep 2003
Location: New Zealand
|
|
|
Thanks, i get a compile error with that and its a little over my head, but thanks for the reply anyways.
|

January 6th, 2004, 07:06 PM
|
 |
Contributing User
|
|
Join Date: Dec 2003
Location: London, ON, Canada, eh
Posts: 127
Time spent in forums: 11 h 11 m 42 sec
Reputation Power: 10
|
|
|
What's the compile error? I'm working on Linux with gcc.
|

January 6th, 2004, 07:12 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Phoenix, AZ
Posts: 60
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Just to let you know, the code works fine for me. I'm on Windows (EGH!) with gcc (MinGW).
TIP: DON'T enter a dollar sign before your price value.
|

January 6th, 2004, 07:15 PM
|
 |
Contributing User
|
|
Join Date: Dec 2003
Location: London, ON, Canada, eh
Posts: 127
Time spent in forums: 11 h 11 m 42 sec
Reputation Power: 10
|
|
|
Does it just look bad or does that cause a compile/run error?
|

January 6th, 2004, 10:13 PM
|
 |
Contributing User
|
|
Join Date: Sep 2003
Location: New Zealand
|
|
|
I get an error on line 14, I have given up trying to enter into a 2d array, cause i hjave had no joy and apparently its not necessary so i am just trying to take a users input and enter it into an array of string.
the problem is incrementing the array position on the next input.
I have been trying something like this:
int main()
{
int amount, count, i, j;
float gst = 0, total = 0;
char prodarray[100]; //array to hold product name
char pricearray[100]; //array to hold prices
cout<<"\n Enter the number of products you have please. MAX 10 \n";
cin>>amount;
while (amount <= 0 || amount > 10)
{ amount = 0;
cout<<"\n INVALID. Your entry must be between 1 - 10 \n";
cin>>amount;
cin.ignore(1);
}
for(count=1; count<=amount;count++)
{ cout<<"\n enter the product NAME \n";
cin.getline(prodarray,100);
cout<<"\n You entered "<<prodarray;
}
|

January 6th, 2004, 10:21 PM
|
 |
Contributing User
|
|
Join Date: Sep 2003
Location: New Zealand
|
|
|
edited
Last edited by DirkPitt : January 6th, 2004 at 10:27 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|