views:

157

answers:

2
int price=' ';   // attempt to grab a decimal number - but not the correct way
int itemnum=' '; // attempt to grab a whole number - but not the right way

while((price== (price*1.00)) && (itemnum == (itemnum*1)))

What is a way to get numbers in 2 diff columns where one column is whole numbers and the other are numbers with decimal places?

A: 

You will need to store the price in a float or double (or long double).

Since you are using C++, you should probably be using the '>>' operators to read the values.

Jonathan Leffler
+1  A: 

The best way would be to get each separately. If it is from a file then you can do this:

int itemnum;
double price;

inputFile >> itemNum >> price; //If the columns are ItemNumber then Price

or

inputFile >> price >> itemnum; //If the columns are the other way around

The >> operator is nice in C++ because it attempts to cast the input to whatever type you are using.

EDIT: Here is a small example for a file:

#include <fstream>
#include <iostream>

int main()
{
    int input1;
    double input2;

    //Open file
    std::ifstream inFile;
    inFile.open("myFile.txt"); //or whatever the file name is

    while(!inFile.eof())
    {
        //Get input
        inFile >> input1 >> input2;

        //Print input
        std::cout << input1 << " " << input2 << " ";
    }

    //Close file
    inFile.close();

    return 0;
}

The file for this could have this data: 120 12.956 121 13.001 1402 12345.8

and the output would be: 120 12.956 121 13.001 1402 12345.8

It will work if the numbers are in columns too.

Mike Webb
i am not getting any input from the user, all the information is in files that i have to read from then write them to another file
@user: Your C++ book should explain in some detail how file streams are used.
James McNellis
yeah i dont understand it and it seems so easy, my teacher told me to see if i can find out how to do it online if i have problems and thats what i am doing. i am not looking for the answers i am looking for help
Did you get the file open successfully? If so then the above code should work.
Mike Webb
nope i never got the files to open correctly when i check to see if the file open correctly but when i dont check the program runs perfectly. i am going to come back to it because i have other things in the program i have to do. Some of my statements are correct and formula is not working
Added a small example of opening a file. This works if the file is in the same folder as the program. Otherwise you have to have the specific path in the filename (i.e. "C:/...path to file.../myFile.txt")
Mike Webb
oh thanks with my luck i would have to add the specific path to open the files, i cant just do it the way that i was previously doing it
+1, Mike - but maybe a loop would help the OP too. Or maybe that's too much like handholding.
Jonathan Leffler
when i use a specific path do i write it in the parenthsis with the argument?
do i have to put the std:: there and if so why?
Loop added. :)You have to use 'std::' if you do not have 'using std;' in the file. I do not use 'using' directives only because I was taught not to. Everyone's style is different, though.You want to include the file path with the file name in the parens. If you just have 'inFile.open("file.txt");' the program will assume to look for file.txt in the same directory as the executable (.exe file in Windows, usually a.out file in Linux).
Mike Webb
am i putting the file path in the () with the file name and is there a symbol i put in to seperate the path from the file name??
Yes and no, respectively. For example, If the file is in a folder called temp on the C drive then the code to open should be 'inFile.open("C:/temp/myFile.txt");'. If on the desktop in Vista and Windows 7 it should be 'inFile.open("C:/Users/myUserDirectory/Desktop/File.txt");' Or if the file is in the same directory as your program just 'inFile.open("myFile.txt");'. Etc. etc...
Mike Webb