[try Beta version]
Not logged in

 
loading a string array and a parallel double array

Jan 29, 2016 at 2:22pm
So i am working on this program that has to read a file contacting property address and the amount of property tax due. The file looks like this:
151 Acorn
500
161 Acorn
1500
200 Main
15000
500 Arcade
25000
181 Acorn
6000
120 Xenia
1000
200 Acorn
20000

1
2
3
4
5
6
7
8
9
10
11
  infile.open("prog3Input.txt");
    if (infile.fail())
        cout << "error opening file\n";
    for (int i =0; i < SIZE; i++)
    {
        getline(infile,address[i]);
        infile >> amtDue[i];
        cout << address[i] << endl;
        cout << "$" << amtDue[i] << endl;
    }


after this executes i get this:

151 Acorn
$500
  
$161
 Acorn
$1500

$200
 Main
$15000

$500
 Arcade
$25000


i used some breakpoints to see what was actually going into the string and i get this:
amtDue double [7]
[0] double 500 500
[1] double 161 161
[2] double 1500 1500
[3] double 200 200
[4] double 15000 15000
[5] double 500 500
[6] double 25000 25000
address string [7]
[0] std::__1::string "151 Acorn\r"
[1] std::__1::string "\r"
[2] std::__1::string " Acorn\r"
[3] std::__1::string "\r"
[4] std::__1::string " Main\r"
[5] std::__1::string "\r"
[6] std::__1::string " Arcade\r"

what the heck am i doing wrong here? i tried a cin.ignor() to no avail.
Last edited on Jan 29, 2016 at 3:04pm
Jan 29, 2016 at 3:07pm
getline(infile,address[i]); => getline((infile >> ws), address[i]);
Jan 29, 2016 at 3:38pm
Awesome that totally worked. Pardon my ignorance but what exactly is that doing, never used 'ws' before.
Jan 29, 2016 at 3:40pm
infile>>ws consumes any whitespace in the stream, if there is any, prior to the next non-whitespace character.
Jan 29, 2016 at 4:31pm
one more question: now that this is working, is there a way to print the arrays on the same line? what i mean is instead of
1
2
cout << adresse[i] <<endl;
cout<< "$" << amtDue[i];

which gives me

151 Acorn
$500

ect, ect

is there a way to get this
 
cout << address[i] << " = $" << amtDue[i] << ends;

to look like this

151 Acorn = $500

instead of showing up like this

151 Acorn
 = $500

it seems like it's still a white space issue
Last edited on Jan 29, 2016 at 4:35pm
Jan 29, 2016 at 5:06pm
Are you saying that cout << address[i] << " = $" << amtDue[i] << endls; is giving you a line break after the address?
Jan 29, 2016 at 5:28pm
yes exactly
Jan 29, 2016 at 5:59pm
You might want to make sure the line endings in the file match the line endings that are used in your coding environment ("\r\n" vs "\n".)

A quick fix would be to just get rid of the last character in the address string.

Jan 29, 2016 at 6:06pm
A quick fix would be to just get rid of the last character in the address string.

How would i go about that? in the file itself, or in the program?
Jan 29, 2016 at 6:11pm
instead of :
 
std::getline(infile >> std::ws ,address);

Try this:
 
std::getline(infile >> std::ws ,address,'\r');

Jan 29, 2016 at 6:25pm
awesome! Thanks man.
just a side note of curiosity; i see a lot of code where using namespace std; isn't used. whats the benefit to that? isn't just more tedious to std:: every time?
anyway thanks again
Jan 29, 2016 at 6:44pm
No Problem..
First)
I know it has worked ... Just to make sure..
(cire) Execuse me.. Is my method that I have given to the OP right ?

Second)
For your question..read this:
http://www.learncpp.com/cpp-tutorial/4-3a-namespaces/


Jan 29, 2016 at 6:53pm
haha my professor finally emailed me back and alerted me that there is a fstream function fileName.ignore(). I like this way better. thanks again fellas
Topic archived. No new replies allowed.