Hi. I need help regarding my homework.
My professor has asked us to design a C++ program that will run in batch mode using Linux redirection. Input will be read in a data file. If characters in data file are valid, we may assume that the rest of the required inout is valid as well. If not, then we should discard the rest of the input line. This is what my code looks like so far:

# include <iostream>
# include <iomanip>
# include <fstream>

using namespace std;

const float LVSUN_BASEPRICE = 700.00;
const float LVRJ_BASEPRICE = 1200.00;
const float USA_BASEPRICE = 3000.00;
const float LVSUN_ADDLINE_CHARGE = 50.00;
const float LVRJ_ADDLINE_CHARGE = 60.00;
const float USA_ADDLINE_CHARGE = 100.00;

int main()
{
  char pubcode; // publication code
  char adcolor; // advertisement color
  int adline; // number of ad lines
  float total_price;
  int num; // number of data lines

  ifstream infile;
  infile.open("input");

  cout << fixed << showpoint << setprecision(2) << endl;

  for (num = 0; num < 4; num ++)
  {
  if (pubcode == 'S')
  {
    if (adline <= 5)
      total_price = LVSUN_BASEPRICE;
    if (adline > 5)
      total_price = LVSUN_BASEPRICE + (LVSUN_ADDLINE_CHARGE * (adline - 5));

    cout << "A" << adline << "black and white advertisement in the Las Vegas Sun will cost $" << total_price << "." << endl;
  }

else
  if (pubcode == 'J')
  {
    if (adline <= 5)
    {
      if (adcolor == 'B')
      total_price = LVRJ_BASEPRICE;
      cout << "A" << adline << "black and white advertisement in the Las Vegas Review Journal will cost $" << total_price << "." << endl;

      if (adcolor == 'C')
      total_price = (LVSUN_BASEPRICE + (LVSUN_BASEPRICE * .50));
      cout << "A" << adline << "color advertisement in Las Vegas Review Journal will cost $" << total_price << "." << endl;


else
    if (adline > 5)
    {
      if (adcolor == 'B')
      total_price = LVRJ_BASEPRICE + (LVRJ_ADDLINE_CHARGE * (adline - 5));
      cout << "A" << adline << "black and white advertisement in the Las Vegas Review Journal will cost $" << total_price << "." << endl;

      if (adcolor == 'C')
      total_price = LVRJ_BASEPRICE + (LVRJ_BASEPRICE * .50) + (LVRJ_ADDLINE_CHARGE * (adline - 5));
      cout << "A" << adline << "color advertisement in the Las Vegas Review Journal will cost $" << total_price << "." << endl;
    }
  }

  else
  if (pubcode == 'U')
  {
    if (adline <= 5)
    {
      if (adcolor == 'B')
      total_price = USA_BASEPRICE;
      cout << "A" << adline << "black and white advertisement in the USA Today will cost $" << total_price << "." << endl;

      if (adcolor =='C')
      total_price = USA_BASEPRICE + (USA_BASEPRICE * .50);
      cout << "A" << adline << "color advertisement in the USA Today will cost $" << total_price << "." << endl;
    }

 else
    if (adline > 5)
    {
      if (adcolor == 'B')
      total_price = USA_BASEPRICE + (USA_ADDLINE_CHARGE * (adline - 5));
      cout << "A" << adline << "black and white advertisement in the USA Today will cost $" << total_price << "." << endl;

      if (adcolor == 'C')
      total_price = USA_BASEPRICE + (USA_BASEPRICE * .50) + (USA_ADDLINE_CHARGE * (adline - 5));
      cout << "A" << adline << "color advertisement in the USA Today will cost $" << total_price << "." << endl;
    }
  }

  else
  if (pubcode != 'S' && pubcode != 'J' && pubcode != 'U')
    cout << pubcode << " is an invalid publication code." << infile.ignore (80, '\n') << endl;
  }
  }
return 0;
}

My input looks like this:
4
S 6
J B 5
X bad data line
U C 12

My output SHOULD look like this:
g++ hw08.cpp
./a.out < input

A 6 line back and hite advertisement in the Las Vegas Sun will cost $750.00.
A 5 line black and white advertisement in the Las vegas Review Journal will cost $1200.00.
X is an invalid publication code.
A 12 line color advertisement in USA Today will cost $5200.00

However, my output right now looks like this:
is invalid publication code.
is invalid publication code.
is invalid publication code.
is invalid publication code.

Is there some code I am missing?
Thank you so much for your help.

It looks like you are not reading data from the file. You need to read in the data and maybe compare it to the character. Maybe something like this

if(getline(infile,pubcode)=='S');
{
      //do something
}

It looks like you are not reading data from the file.

I agree.

Maybe something like this

if(getline(infile,pubcode)=='S');
{
      //do something
}

I disagree. getline() doesn't return the read data. I guess you mean something like:

string in;
getline(infile, in);
if (in == "something") //<-- double quotes here for string type
{
    //do stuff

I disagree. getline() doesn't return the read data. I guess you mean something like:

Thanx for the correction. Thought you would do it all in one go :)

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.