function call problem

class assignment. copy and paste the code below including function call. everyone's worked except mine. they were using macs, I am using a surface book. output ignores the cities. instructor couldn't figure out the problem. infile.ignore issule? config with windows vs mac? I can't figure it out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  int main() {
double fahren, celsius;
int value1 = 25;
string city;
ifstream infile("temps.txt");

    if (!infile) {
        cout << "File not found\n";
    }
    while (getline(infile, city)) {
        infile >> fahren;
        infile.ignore();
        celsius = fahrenToCel(fahren);
        cout <<"The Celsius temp in \t"<<city<<"is" " "<<celsius<<endl;
        cout<<"The celsius temp in \t"<<city<<"is" <<celsius<<endl;
    }

    cout << "\nBefore running passByValue value1 = " << value1 << endl;
    passByValue(value1);
    cout << "After running passByValue value1 = " << value1 << endl;

    cout << "Before running passByReference value1 = " << value1 << endl;
    passByReference(value1);
    cout << "After running passByReference value1 = " << value1 << endl;

    return 0;
}

convert.h

#ifndef CONVERT_H
#define CONVERT_H
//function prototypes belong in .h file
double fahrenToCel(double fahren);
void passByValue(int value);
void passByReference(int &ref);
#endif

#include "convert.h"
double fahrenToCel(double fahren)
{
    double result = (fahren - 32) * (5.0 / 9.0);
    return result;
}

void passByValue(int value)
{
    value++;
}

void passByReference(int &ref)
{
    ref++;
}

temps.txt
New York
65
Saint Paul
32

output:
is 18.3333
is18.3333
The Celsius temp in 	is -17.7778
The celsius temp in 	is-17.7778

Before running passByValue value1 = 25
After running passByValue value1 = 25
Before running passByReference value1 = 25
After running passByReference value1 = 26


I haven't looked at the code seriously, just quickly in passing.

What strikes me is the one thing expected to be a difference between MAC and Windows.

In MAC, which is actually UNIX (and in Linux), the text file lines are terminated with a line feed.

In Windows, they are terminated with a carriage return and then a line feed.

If you took the file from a MAC source, it may be line feed terminated, only.

If THEY took the file from a Windows source, THEY may be getting the impression that every other line is blank.

It's late, I'm not thinking about this all that seriously, but I'd thought I'd play off the point you made about the different operating systems.

They work the same if the source files are as expected for each, but when one takes a file from *nix into Windows, or from Windows into *nix, this one difference can cause issues.
Try

 
        infile.ignore(999, '\n');

infile.ignore() uses the default values 1 and eof(), so it reads exactly one character, which we are hoping is the '\n' that needs to be removed. This is not a good way to use the function.

infile.ignore(999, '\n') says to read up to 999 chars, but to stop reading once it's read a '\n'.

You are saying it works on Macs, where the common modern line-ending is a simple '\n' (like any *nix). On windows, the line ending is '\r''\n'. I can't test it (easily) right now, but it may be that, even in text mode where '\r''\n' is converted to '\n' on input, infile.ignore() will only read the '\r' and leave the '\n' behind.
Last edited on
tried it. it corrected the math problem but still omitting the cities

1
2
3
4
5
6
7
8
9
 is 18.3333
 is 0

Before running passByValue value1 = 25
After running passByValue value1 = 25
Before running passByReference value1 = 25
After running passByReference value1 = 26

Process finished with exit code 0
@bigskit13, show all your headers, please. Also, if you show output make sure that it is from the code that you have presented. (Which your last wasn't.)

Also, open a text file with a simple editor (e.g. notepad, if you are using Windows) and type in temps.txt manually. (Don't cut and paste it from elsewhere or download it.) Make sure that there are NO BLANK LINES in your file.

What does this give you on your machine?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

double fahrenToCel(double fahren)
{
    double result = (fahren - 32) * (5.0 / 9.0);
    return result;
}

void passByValue(int value)
{
    value++;
}

void passByReference(int &ref)
{
    ref++;
}


int main()
{
   double fahren, celsius;
   int value1 = 25;
   string city;
   //ifstream infile("temps.txt");
   istringstream infile( "New York\n"
                         "65\n"
                         "Saint Paul\n"
                         "32\n" );

   if (!infile) {
       cout << "File not found\n";
   }
   while (getline(infile, city))
   {
      infile >> fahren;
      infile.ignore( 999, '\n' );
      celsius = fahrenToCel(fahren);
      cout << "The Celsius temp in " << city << " is " << celsius << endl;
   }

   cout << "\nBefore running passByValue value1 = " << value1 << endl;
   passByValue(value1);
   cout << "After running passByValue value1 = " << value1 << endl;

   cout << "Before running passByReference value1 = " << value1 << endl;
   passByReference(value1);
   cout << "After running passByReference value1 = " << value1 << endl;
}
Last edited on
> tried it. it corrected the math problem but still omitting the cities
show your updated code.

run your code through a debugger
watch your `city' and `fahren' variables there

with your first code, my guess is
first iteration, city = "New York\r", fahren = 65
second iteration, city = "", fahren = read_error

also, you may do an hexdump of your "text" file
sorry about that gents, could've sworn i did post the code. here it is after simply adding the infile.ignore(999,'\n,); does the math correct but still won't display the temps.txt.
Everything manually entered, checked for spaces etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <fstream>
#include "convert.h"
using namespace std;
int main() {
    double fahren, celsius;
    int value1 = 25;
    string city;
    ifstream infile("temps.txt");

    if (!infile) {
        cout << "File not found\n";
    }

    while (getline(infile, city)) {
        infile >> fahren;
        infile.ignore(999, '\n'); //<-- added 999 and '\n'
        celsius = fahrenToCel(fahren);
        cout << "The celsius temp in " << city << " is " << celsius << endl;
    }

    cout << "\nBefore running passByValue value1 = " << value1 << endl;
    passByValue(value1);
    cout << "After running passByValue value1 = " << value1 << endl;

    cout << "Before running passByReference value1 = " << value1 << endl;
    passByReference(value1);
    cout << "After running passByReference value1 = " << value1 << endl;

    return 0;

temps.txt:

New York
65
Saint Paul
32

#ifndef CONVERT_H
#define CONVERT_H
//function prototypes belong in .h file
double fahrenToCel(double fahren);
void passByValue(int value);
void passByReference(int &ref);

double fahrenToCel(double fahren)
{
    double result = (fahren - 32) * (5.0 / 9.0);
    return result;
}

void passByValue(int value)
{
    value++;
}

void passByReference(int &ref)
{
    ref++;
}

Output:
 is 18.3333
 is 0

Before running passByValue value1 = 25
After running passByValue value1 = 25
Before running passByReference value1 = 25
After running passByReference value1 = 26

Process finished with exit code 0
}
Last edited on
last chance. your code worked beautifully. It has some coding that I haven't been taught yet. I'll try a to debug to figure out why the other code didn't work on my machine when it did for everyone else. I can't seem to make the \r work in any way shape or form. Funny thing is both the TA and instructor couldn't figure it out which means I'm not going to I bet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <sstream>
#include <string>
#include "convert.h"
using namespace std;


int main()
{
    double fahren, celsius;
    int value1 = 25;
    string city;
    //ifstream infile("temps.txt");
    istringstream infile( "New York\n"
                          "65\n"
                          "Saint Paul\n"
                          "32\n" );

    if (!infile) {
        cout << "File not found\n";
    }
    while (getline(infile, city))
    {
        infile >> fahren;
        infile.ignore( 999, '\n' );
        celsius = fahrenToCel(fahren);
        cout << "The Celsius temp in " << city << " is " << celsius << endl;
    }

    cout << "\nBefore running passByValue value1 = " << value1 << endl;
    passByValue(value1);
    cout << "After running passByValue value1 = " << value1 << endl;

    cout << "Before running passByReference value1 = " << value1 << endl;
    passByReference(value1);
    cout << "After running passByReference value1 = " << value1 << endl;

}
The Celsius temp in New York is 18.3333
The Celsius temp in Saint Paul is 0

Before running passByValue value1 = 25
After running passByValue value1 = 25
Before running passByReference value1 = 25
After running passByReference value1 = 26

Process finished with exit code 0
@bigskit13,
If that code works then comment out the stringstream lines (which are just simulating an input stream) and uncomment the ifstream line (so that you are returning to the file input). You will also need to reinstate #include <fstream> . At this point you should be dependent on the form of the input file.

The code works fine when reading from file on my Windows 7 PC.

Are you absolutely sure that you have typed a new, clean input file with your editor? And that this is the file being read by the program? Try changing one of the temperatures in that file to make sure that you are reading the particular file that you think you are and not some legacy file that you copied earlier.
Last edited on
Output:
 is 18.3333
 is 0
again, look your variables through a debugger
if you happen to have an '\r' or another weird character at the end of the string, you may remove it with
1
2
if (city.back() == '\r')
   city.pop_back();


> Everything manually entered, checked for spaces etc.
hexdump
uncommented the ifstream ane reinstated fstream and the input file. Also verified that it's reading the correct file by changing the temps. Still won't show the cities.
ne555 that worked! I'm using CLion and couldn't see anything on the debugger. I'm new though so that isn't saying much. can you explain to me what happened or how this works?

THANK YOU!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (!infile) {
        cout << "File not found\n";
    }
    while (getline(infile, city))
    {
        if (city.back() == '\r')
            city.pop_back();
        infile >> fahren;
        infile.ignore( 999, '\n' );
        celsius = fahrenToCel(fahren);
        cout << "The Celsius temp in " << city << " is " << celsius << endl;
    }

The Celsius temp in New York is 18.3333
The Celsius temp in Saint Paul is 0

Before running passByValue value1 = 25
After running passByValue value1 = 25
Before running passByReference value1 = 25
After running passByReference value1 = 26
Last edited on
for anyone coming through with the same problem. Here's the background on my issue.
https://en.wikipedia.org/wiki/Newline
Topic archived. No new replies allowed.