Hello All

I an taking a class in C++. Though I have had many years programming, it is an introductory class because one needs to learn the syntax and rules of the new language.

I am writing a program that stores data in a file, and retrieves data from a file and I need to format that data so it will have a structure. What I have elected to do is ask the user for how many numbers to store in the datafile, and then the code will generate that many "random" numbers, calculate the square root, squared, and cubed values of that these 'random" numbers and store the data in columns.

My questions are:

1) How do I make the retrieval code get the "random" number , its square root, squared, and cubed values when these are all on the same line? Is there a way to use something like:

inFile >> num1;
inFile >> num2; and have this line read the next value over on the same line vs. one line below. BTW inFile is set to ifstream to read from the data file using the preprocessor directive #include<fstream>

Do I need to read the whole line and then parse it, and how would one parse a tab delimited line of text?

2) I know what showpoint does, it forces the display of the decimal point, but can I selectively turn it on and off and how?

Thanks for helping me understand more about C++ and its finer points.

Since you already know how many numbers are on a line

while( inFile >> num1 >> num2 >> num3 >> num4)
{
   // do something with the data
}

1) cin's >> operator will stop reading a single item when it hits whitespace. Ideally you would use getline to read the whole line, then stringstream to break it apart:

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

int main()
{
  std::string line;

  while ( getline ( std::cin, line ) ) {
    std::stringstream in ( line );
    int r, sqr, sq, cube;

    if ( in>> r >> sqr >> sq >> cube ) {
      // Process the values
    }
    else {
      std::cerr<<"Invalid input format\n";
      break;
    }
  }
}

This way you can actually error check each line. If you simply use >> in a loop, even newlines will count as regular whitespace, so you may cross the line barrier without knowing it if an erroneous line contains fewer than expected values.

2) Yes, there are the std::showpoint and std::noshowpoint manipulators in <ios>. You can also do it with the setf and unsetf member functions of your stream. Or you can use the std::setiosflags function from <iomanip>.

Thank You Ancient Dragon,

I will look into this and send more questions once they arise.

Thanks again,

Cordially

Waseem

Thank You Narue

Wow!!! I love the code, color, line numbers what one can ask for?

Could you tell me how this was done so that if I need to post any code snipits they would look nice and readable like yours?

Thanks for explaining the code too, will definitely come in handy.

Cordially

Waseem

>Could you tell me how this was done
On the upper right hand corner of the code block is a link that says "Help with Code Tags". Click that for a decent tutorial.

This article has been dead for over six months. Start a new discussion instead.