C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 6th, 2004, 03:56 AM
DirkPitt's Avatar
DirkPitt DirkPitt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: New Zealand
Posts: 295 DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 5 h 30 m 10 sec
Reputation Power: 17
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

Reply With Quote
  #2  
Old January 6th, 2004, 10:13 AM
idologic_aw's Avatar
idologic_aw idologic_aw is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: London, ON, Canada, eh
Posts: 127 idologic_aw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

}

Reply With Quote
  #3  
Old January 6th, 2004, 06:14 PM
DirkPitt's Avatar
DirkPitt DirkPitt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: New Zealand
Posts: 295 DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 5 h 30 m 10 sec
Reputation Power: 17
Thanks, i get a compile error with that and its a little over my head, but thanks for the reply anyways.

Reply With Quote
  #4  
Old January 6th, 2004, 07:06 PM
idologic_aw's Avatar
idologic_aw idologic_aw is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: London, ON, Canada, eh
Posts: 127 idologic_aw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old January 6th, 2004, 07:12 PM
aerea aerea is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Phoenix, AZ
Posts: 60 aerea User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
Send a message via AIM to aerea
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.

Reply With Quote
  #6  
Old January 6th, 2004, 07:15 PM
idologic_aw's Avatar
idologic_aw idologic_aw is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: London, ON, Canada, eh
Posts: 127 idologic_aw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #7  
Old January 6th, 2004, 10:13 PM
DirkPitt's Avatar
DirkPitt DirkPitt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: New Zealand
Posts: 295 DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 5 h 30 m 10 sec
Reputation Power: 17
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;
}

Reply With Quote
  #8  
Old January 6th, 2004, 10:21 PM
DirkPitt's Avatar
DirkPitt DirkPitt is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: New Zealand
Posts: 295 DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level)DirkPitt User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 5 h 30 m 10 sec
Reputation Power: 17
edited

Last edited by DirkPitt : January 6th, 2004 at 10:27 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > user input to 2d array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap