wrong answer in division

i have a code that is running fine except that when it reads in from the third line of the input file the answer it prints is wrong. its supposed to give an answer of 1 4/5 as output but what the program gives for an answer is 1 7/10. the program im working at is supposed to read from an input file containing decimal numbers(ex. 0.5 or 1.25) and the output should be in the mixed fraction format. my problem is that the first two lines read in have been processed correctly except for the last line which is 1.8 and should give out an answer of 1 and 4/5 but instead is giving me 1 and 7/10 for an answer. Can anyone help me please and point out what is wrong with my code. Thanks.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;

int gcd(int x,int y);

main(){
  ifstream file_in;
  string   strns , str, s1 , mv,
           strs[100], c;
  double   x , y , z, a ,
           num, b , d ;     
  int      e , v , w ,f , g;
  file_in.open("probf.in");
  z = 0;
  while( !file_in.eof() ){
     getline( file_in , s1);
    
     if(z==0){
         a = atoi(s1.c_str());
           }
     else {
          num = atof(s1.c_str());          
          c = s1.substr(0 , s1.find('.'));
          b = atoi(c.c_str());      //whole number
          d = num - b;              // decimal part
          x = d * 10;
          y = 10;
          v = int(x); 
          w = int(y);
          e = gcd(v,w);
          f = v / e;
          g = w / e;         
           cout << b <<" "<<  f <<"/ "<< g << endl;         
          }    
         z++;
        }
  cout << endl;
  system("PAUSE");
}
 
int gcd(int x,int y){
    int temp;
    
     if ( y == 0 )
          return x; 
     else if ( x >= y && y > 0 )
          return gcd(y, x%y);
     else {
        temp = x;
        x = y;
        y = temp;
        return gcd(y, x%y);
        }          
     }
Hi particle,

I compiled your code and found that I get 1 4/5 when the program reads 1.8. I did have to add code to check the result of getline() to make sure that it succeeded (which it doesn't if it has read the last line.)

How many entries are in your input file? Can you try putting 1.8 as the first input value to see if it works there?
kooth,

thanks for trying it out, but my compiler doesn't give me the correct answer. I'm using the latest stable version of dev c++, and my input file has four entries. the first line tells the program how many lines to process, and the rest are all the datas to be processed.

input file content:
3
1.5
0.25
1.8
I'm using the latest stable version of dev c++

IIRC, that means it's five years old. Don't use Dev-C++. Get VC++ Express or Code::Blocks instead.
felipe,

thanks for pointing out the outdated compiler, but then again it doesn't answer the question as how it happened that i got 2 correct data processed and one giving me a wrong answer. I've compiled the same program on a visual c++ compiler(VC++ express 2008) and it gave me the same answer. So what's the problem with the code? I hope someone can help me out on this as I need to understand why im not getting it right for the last line processed. help anyone?
Honestly, your variables are so cryptically named I can't bring myself to try to read your code. There's also at least one useless variable that seems to be there just to make it less readable (y in main() (which should be int main())). And in C++ you don't need to declare all the variables at the top of the function. You should declare them as close as possible to their use.

And don't loop on eof() ( http://www.cplusplus.com/forum/general/30918/#msg168005 ).

And don't use system() calls ( http://www.cplusplus.com/forum/articles/11153/ ).
Topic archived. No new replies allowed.