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 5th, 2006, 03:24 PM
lamefif lamefif is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 lamefif User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 13 m 18 sec
Reputation Power: 0
Open file populate array, not as easy as it sound first time round

here is my attempt at loading a file:
dont laugh

Code:
if(action=="open"){ 
ifstream imput("outfile.txt"); for(int i=0;i<6;i++){ 
string tempName=""; 
float temppPrice=0,tempsPrice=0;
 int tempQ=0;
 imput >>tempName >> temppPrice >> tempsPrice>>tempQ; 
sarray[i].setName(tempName); sarray[i].setpPrice(temppPrice); 
sarray[i].setsPrice(tempsPrice); sarray[i].setQuantity(tempQ); 
} 
cout<<"done"<<endl; } 
}


file format:
Red Stuff
10.5
5
20.5
Green Stuff
10.5
5
20.5
Yellow Stuff
10.5
5
20.5
Orange Stuff
10.5
5
20.5
Pepper Stuff
10.5
5
20.5
Suger Stuff
10.5
5
20.5
but im getting this:
----------------------------------------------------------
Pruduct 1 Red | 0 | 0 | 0
----------------------------------------------------------
Pruduct 2 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 3 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 4 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 5 | 0 | 0 | 0
----------------------------------------------------------
Pruduct 6 | 0 | 0 | 0
----------------------------------------------------------
instead of:
----------------------------------------------------------
Pruduct 1 Red Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 2 Green Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 3 Yellow Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 4 Orange Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 5 Pepper Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------
Pruduct 6 Suger Stuff | 10.5 | 20.5 | 5
----------------------------------------------------------

thanks

Reply With Quote
  #2  
Old January 5th, 2006, 04:49 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,906 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 31 m 41 sec
Reputation Power: 1774
The basic >> operator stops at white space, so everything stops when you've got past "Red".

Try
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
  ifstream imput("outfile.txt");
  for(int i=0;i<6;i++){
    char tempName[100]="";
    float temppPrice=0,tempsPrice=0;
    float tempQ=0;
    imput.getline(tempName,sizeof tempName);  /* read a whole line */
    imput >> temppPrice >> tempsPrice>>tempQ; /* read numeric data */
    imput.ignore();                           /* skip to start of next line */
    cout << tempName << temppPrice << tempsPrice << tempQ << endl;
  }
}

Reply With Quote
  #3  
Old January 5th, 2006, 05:33 PM
lamefif lamefif is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 lamefif User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 13 m 18 sec
Reputation Power: 0
Thank you, you have saved me 3-5 hours, a lot of energy waste and thousands of brain sells from being fried.
i thank you

ps. Do you know of a way to prompt a file dialog to specify the file name, instade of asking the user to type it in the command line;
thax again

Reply With Quote
  #4  
Old January 6th, 2006, 03:34 PM
ComputerPhreak's Avatar
ComputerPhreak ComputerPhreak is offline
Super User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 648 ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 21 h 32 m 25 sec
Reputation Power: 74
Quote:
Originally Posted by lamefif
Thank you, you have saved me 3-5 hours, a lot of energy waste and thousands of brain sells from being fried.
i thank you

ps. Do you know of a way to prompt a file dialog to specify the file name, instade of asking the user to type it in the command line;
thax again


That's platform specific, what OS are you using?

Reply With Quote
  #5  
Old January 6th, 2006, 09:22 PM
lamefif lamefif is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 5 lamefif User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 13 m 18 sec
Reputation Power: 0
XP

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Open file populate array, not as easy as it sound first time round

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