-1

I am attempting to read a .txt file. I am tasked with using only:

   #include <iostream>

I must use redirect.

Multiple lines of data, such as:

AddItem 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8

exist in the file.

I need to read in the "AddItem" as a Char array, and the values as ints and doubles. Here is my code.

#include <iostream>
using namespace std;

void emptyString(char* x, int size) {
   for (int i = 0; i < size; i++)
       x[i] = '\0';
}

int main() {
    char command[10];
    int quantity, code, brand, type, option, option2;
    double height, length, width, weight, price;

    while (!cin.eof()) {// while end of file is not reached
        emptyString(command, 10);
        cin >> command 
        >> quantity 
        >> code 
        >> brand 
        >> height 
        >> length
        >> width
        >> weight
        >> price
        >> type
        >> option
        >> option2;
    
    if (!cin.eof()) {
        cout << command << ", "
            << quantity << ", "
            << code << ", "
            << brand << ", "
            << height << ", "
            << length << ", "
            << width << ", "
            << weight << ", "
            << price << ", "
            << type << ", "
            << option << ", "
            << option2 << ", "
            << endl;
        }
    }
    return 0;
}

When I run the file it never ends. ctrl+z is supposed to stop it, but that does nothing for me in Visual Studio 2015.

My output looks like this: Output

Community
  • 1
  • 1
  • 1
    First, see [this question regarding eof](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). Second, your code is vulnerable to buffer overruns. Finally, you make no attempt to handle invalid input. Most likely a read is failing and leaving `cin` in a fail state. Is your file required to have all of those values for each item? If any are missing you will probably have a failed read. – Greg Kikola Feb 06 '17 at 04:28
  • The file is supposedly in tact. Currently I am just trying to get the data to read, I plan to handle fails once I can get correct data reading in. Just trying to work in steps. – onelonglizard Feb 06 '17 at 04:38
  • Besides the EOF thing that @GregKikola mentioned, you're reading things out of order. This means that you attempt to read "4.97" as an `int` and the rest of your reads fail. That leaves the following variables uninitialized. Since you then try to print the uninitialized values, the behavior of your program is undefined. – Miles Budnek Feb 06 '17 at 04:57
  • Can you explain how I am reading things out of order. I understand 4.97 would definitely not work well with an int datatype, but I have 'price' which should receive the 4.97 value defined as a double. I understand it's reading it as an int according to the output, I just can't find my error. – onelonglizard Feb 06 '17 at 05:15
  • @onelonglizard They don't match up: you're trying to read `4.97` into `type`. The next read will fail, which will cause subsequent reads to fail. You should reorganize your code to make things easier to read and maintain. – Greg Kikola Feb 06 '17 at 05:32

1 Answers1

0

I would try something like this:

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

using std::cout; using std::endl;
using std::cin; using std::getline;
using std::string; using std::istringstream;
using std::vector;

void process_command(const string& cmd, const vector<double>& args);

int main()
{
  string line, command;
  while (getline(cin, line)) { //read input one line at a time
    istringstream ss(line); //use stringstream to read from the line
    ss >> command;

    vector<double> args;
    double cur_arg;
    while (ss >> cur_arg) //read arguments until we hit the end
      args.push_back(cur_arg); //add argument to vector

    //process the command                                                       
    process_command(command, args);
  }

  return 0;
}

void process_command(const string& cmd, const vector<double>& args)
{
  cout << "Command: " << cmd << "\n"
       << "Arguments: ";
  for (auto s : args)
    cout << s << " ";
  cout << endl;
}

Example input:

AddItem 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8
DoStuff 37 -123 0.235
DoMoreStuff 7 1e6 23 0.418

Example output:

Command: AddItem
Arguments: 3 1 1 4 7.75 7.62 0.69 0.025 4.97 2 0 8 
Command: DoStuff
Arguments: 37 -123 0.235 
Command: DoMoreStuff
Arguments: 7 1e+06 23 0.418 

Maybe that will give you some ideas.

Greg Kikola
  • 538
  • 3
  • 6
  • I really appreciate the effort! Unfortunately, I am constrained to using only #include . I would absolutely love to do it your way if only I could. – onelonglizard Feb 06 '17 at 05:16