//project.cpp  //compile error-price undeclared (first use this function)         
#include <iostream>       //don't I have it declared in the float line??
#include <fstream.h>
#include <stdlib.h>
using namespace std;
int main()
//PROBLEM:  Create an input file of recently shipped music CD's. The file  should consist of
// the name of the album, quantity shipped and price. Since we do not know how many CD's 
//have been shipped, keep reading the file until all CD's have been read in.  Keep track of
// both the number of different CDs as well as the total number of CDs shipped.
//Your program should determine the value of each CD shipped (quantity * price).
//Create an array to hold this value and print out all values in the array.  
{
  float CD[5];            
  int num;
  float qty=0, val=0; price=0;
  ifstream incd;
  ofstream outcd;

  incd.open("incd.txt");
  outcd.open("outcd.txt");

  if (incd.fail())
  {
    cout<<"Output file doesn't exist!";
    exit(1);
  }
  while (!incd.eof())
  {
    incd>>CD[num]; //how does it read in qty & price and distinguish them??
    for (num=0; num<5; num++)    
    {
      incd>>CD[num];  
      qty = CD[num] + qty;
      val = qty * price;  
    }    

    outcd<<"The value of this CD is: "<<val<<endl;
    val = 0;
    qty = 0;
    price = 0;
  }              
  num = incd.eof ();    //same as while statement???  

  system("PAUSE");
  incd.close();
  return 0;
}

Dani AI

Generated

As noted, the immediate compile error came from a variable-declaration mistake that left price undeclared. Fixing that will let the program compile, but there are several other issues in the posted code that will cause wrong behavior or crashes: an uninitialized index (num) used as an array index, using a fixed-size CD[5] when the file length is unknown, using while(!eof()) for input control, the obsolete <fstream.h> header, and fragile parsing of album names that contain spaces. A clearer, safer approach is to (1) check the file open, (2) read one record per line and parse it, (3) accumulate per-record value (qty * price) into a dynamically sized container, and (4) avoid system("PAUSE").

A compact example that implements those ideas and prints all per-CD values:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>

int main() {
    std::ifstream in("incd.txt");
    if (!in) { std::cerr << "incd.txt not found\n"; return 1; }

    std::string line;
    std::vector<double> values;
    int totalQty = 0;

    while (std::getline(in, line)) {
        if (line.empty()) continue;
        std::istringstream iss(line);
        std::vector<std::string> tok;
        std::string w;
        while (iss >> w) tok.push_back(w);
        if (tok.size() < 3) continue; // malformed
        double price = std::stod(tok.back());
        int qty = std::stoi(tok[tok.size()-2]);
        values.push_back(qty * price);
        totalQty += qty;
    }

    for (size_t i = 0; i < values.size(); ++i)
        std::cout << "Value " << i << ": $" << std::fixed << std::setprecision(2) << values[i] << '\n';

    std::cout << "Distinct CDs: " << values.size() << "  Total shipped: " << totalQty << '\n';
    return 0;
}

Quick troubleshooting notes: initialize any index variables before use (or avoid them by using vector::push_back), prefer <fstream> and std::getline for robust parsing, avoid while(!eof()) (use the extraction or getline result instead), and for real money calculations prefer integer cents or a decimal library instead of float to avoid rounding errors. As pointed out, fixing the declaration error was the first step; the example above addresses the remaining logic and parsing issues.

Recommended Answers

All 2 Replies

>don't I have it declared in the float line??
No. If you had preceded it with a comma instead of a semicolon then yes:

float qty=0, val=0[B];[/B] price=0;

Thank you, what a stupid mistake

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.