I'm having trouble reading from my files properly.
the files being read from is in this format:
Q:3
D:4
N:1
and that is all in the file, it stands for quarters, dimes, nickels. yeah
and the function i have to read from that is this:

void initialize (ifstream& soda, ifstream& coin, SodaMachine* soda_machine)
{
    string coin_line;
    char coin_type;
    char temp_char;
    int num_coins;
   
   do
   { 
   //Read in line from file
   coin >> coin_type;
   
   //Check to make sure coin_type is a valid type
   switch (coin_type)
   {
        case 'Q':
            coin_type = 'q';
            break;
        case 'q':
            
            coin >> temp_char;
            if (temp_char==':')
            {
                coin >> num_coins;
                
                if (num_coins >= 0)
                {
                    soda_machine->quarters=num_coins;
                }
                else
                {
                    //num_coins is less than 0 or soda failed
                }   
            }
            else
            {
                //Second character was not a colon
            }
            break;
        case 'D':
            coin_type = 'd';
            break;
        case 'd':
            
            coin >> temp_char;
            if (temp_char==':')
            {
                coin >> num_coins;
                
                if (num_coins >= 0)
                {
                    soda_machine->dimes=num_coins;
                }
                else
                {
                    //num_coins is less than 0 or soda.cin failed
                }   
            }
            else
            {
                //Second character was not a colon
            }
            break;
        case 'N':
            coin_type = 'n';
            break;
        case 'n':
            
            coin >> temp_char;
            if (temp_char==':')
            {
                coin >> num_coins;
                
                if (num_coins >= 0)
                {
                    soda_machine->nickels=num_coins;
                }
                else
                {
                    //num_coins is less than 0 
                }   
            }
            else
            {
                //Second character was not a colon
            }
            break;
        case '@': //should be default
            //First letter in line is not Q, q, D, d, N, or n
            break;
    }
}
while (coin);
}

There is a soda_machine is a struct that has an array of dispensers, and the money stored inside the soda machine.

instead of getting numbers like 4 and 5 for the quantity of numbers
i get numbers like 2349183640.
can anyone suggest something?
thanks

Recommended Answers

All 3 Replies

void initialize (ifstream& soda, ifstream& coin, SodaMachine* soda_machine)
{    
     string coin_line;    
     char coin_type;    
     char temp_char;   
     int num_coins;    
     //do  
     //{   
           //Read in line from file   
           coin >> coin_type;    
           //Check to make sure coin_type is a valid type   
           switch (coin_type)   
          {         
               case 'Q':           
                     coin_type = 'q';// remove this if you don't need it      
               //--remove  break;        
               case 'q':             
                      coin >> temp_char;           
                      if (temp_char==':')            
                      {                
                             coin >> num_coins;                
                             if (num_coins >= 0)                
                             {                    
                                   soda_machine->quarters=num_coins;                
                             }                
                             else                
                             {                   
                                   //num_coins is less than 0 or soda failed                
                             }      
                      }
                      else            
                      {
                              //Second character was not a colon            
                      }            
                      break;        
                case 'D':            
                      coin_type = 'd';// remove this if you don't need it       
                      //--remove break;        
                case 'd':             
                      coin >> temp_char;            
                      if (temp_char==':')            
                      {                
                             coin >> num_coins;                 
                             if (num_coins >= 0)                
                             {                    
                                    soda_machine->dimes=num_coins;                
                             }                
                             else                
                             {                    
                                    //num_coins is less than 0 or soda.cin failed                
                             }               
                      }            
                      else            
                      {                
                             //Second character was not a colon            
                      }            
                      break;        
                case 'N':            
                      coin_type = 'n';// remove this if you don't need it   
                      //--remove break;        
                case 'n':             
                      coin >> temp_char;            
                      if (temp_char==':')            
                      {                 
                            coin >> num_coins;                 
                            if (num_coins >= 0)                
                            {                    
                                    soda_machine->nickels=num_coins;                
                            }                
                            else                
                            {                    
                                    //num_coins is less than 0                 
                            }               
                      }            
                      else            
                      {                
                            //Second character was not a colon            
                      }            
                      break;        
                 case '@': 
                      //should be default            
                      //First letter in line is not Q, q, D, d, N, or n            
                      break;    
              }
        //}while (coin);
}

I don't understand what you initialize() function initialized. For example, why the 1st parameter is unused ifstream soda...
Look at the snippet below. You may adapt it for your needs...

/* Includes (if compiled w/o stdafx.h):
#include <stdio.h>
#include <limits.h>

#include <iostream>
#include <fstream>
#include <string>
*/
using namespace std;
/// Sum in cents value wrapper.
class Cents
{
public:
    Cents(int val = 0):value(val>0?val:0)
    {}
    operator int() const { return value; }
    Cents& operator =(int val)
    {
        value = (val > 0? val: 0);
        return *this;
    }
    Cents& operator +=(const Cents& c)
    {
        value += c.value;
    }
    // and so on...
private:
    int value;
};
/// istream >> Cents operator
istream& operator >>(istream& is, Cents& coin)
{
    string line;
    int val = 0;
    
    if (getline(is,line))
    {
        char type;
        if (sscanf(line.c_str(),"%c:%d",&type,&val) == 2)
        {// Let old good sscanf converts type:number...
            double factor = 0.0;
            switch (type)
            {
            case 'Q': case 'q':
                factor = 25.0;
                break;
            case 'D': case 'd':
                factor = 10.0;
                break;
            case 'N': case 'n':
                factor = 5.0;
                break;
                // Free gifts:
            case 'C': case 'c':
                factor = 1.0;
                break;
            case '$':
                factor = 100.0;
                break;
            default: // is.setstate(ios::failbit);
                break;
            }
            // Prevent possible overflow...
            double dval = factor * val;
            if (dval > 0 && dval <= INT_MAX)
                val = static_cast<int>(dval);
            else // throw (INT_MAX)?
                val = 0;
        }
    }
    coin = val;
    return is;
}
/// Example: print out type:number file
int main(int argc, char* argv[])
{
    if (argc > 1)
    {
        ifstream f(argv[1]);
        Cents cents;
        for (int lineno = 1; f >> cents; ++lineno)
        {
            cout << lineno << ":\t" << cents << "c\n";
        }
        cout.flush();
    }
	return 0;
}

okay, thanks a lot.
That helped. many thanks.
many many many thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.