Assigned Return Statement is Different Than Return Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orangemonkey
    New Member
    • Apr 2007
    • 16

    Assigned Return Statement is Different Than Return Statement

    I basically have a function that returns an int. I know that the value of the int should be 6, but when I assign the function to an int variable, that int variable has a value different than 6.

    Or in other words

    int main(void)
    {
    int ff= 0;
    ff= f1();
    cout << ff;
    }

    int f1 ()
    {return 6;}

    Output
    2342342424

    Thanks!
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    It works fine for me:[code=cpp]#include <iostream>

    int f1();

    int main()
    {
    int ff=0;
    ff=f1();
    std::cout << ff;
    }

    int f1()
    [/code]I'm guessing, this isn't exactly what you checked or is there something you forgot? What is the exact code you used?

    Greetings,
    Nepomuk

    Comment

    • orangemonkey
      New Member
      • Apr 2007
      • 16

      #3
      EDIT : Hey. I solved my problem. It was a problem with recursion. Stupid me :P.
      Thanks !


      Hey thanks very much. This problem is very strange :p

      here's the input, code (important parts bolded) and output

      the input
      Code:
      105
      14
      16
      IIIIIIIIIIIIIIII
      I......I.......I
      I......III.....I
      I........I.....I
      I........IIIIIII
      IIIIIIIIII.....I
      I.I......I.....I
      III..III.I.....I
      I....I.IIIII...I
      I....I.....III.I
      I....I.......I.I
      I....I.....III.I
      I....I.....I...I
      IIIIIIIIIIIIIIII
      the code
      Code:
      #include <iostream>
      #include <vector>
      using namespace std;
      int count(vector <vector<string> > &v2, int r, int c, int fcount,int sr, int sc);
      void show (vector <vector<string> >  v2, int r , int c);
      
      int main (void)
      {
      
      
      
      int f=0,r=0,c= 0;
      cin >> f;
      cin >> r;
      cin >> c;
      
      
      cin.ignore();
      
      vector <string> v(c);
      vector <vector<string> > v2(r,v);
      
      string s1 = "";
      
      for(int i=0;i<r;i++)
      {
      getline(cin,s1);
      for(int i2=0;i2<s1.length();i2++)
      {
      v2[i][i2]=s1[i2];
      }
      }
      
      int fcount = 0;
      [B]int ff = 0;[/B]
      
      v2[1][1]="X"; fcount++;
      [B]ff = count(v2,1,1,fcount,r,c);[/B]
      show(v2,r,c);
      [B]cout << ff;[/B]
      
      
      }
      
      int count(vector <vector<string> >& v2, int r, int c, int fcount, int sr, int sc)
      {
      //right
      if(v2[r][c+1]==".")
      {v2[r][c+1]="X";fcount++;cout<<"wee";count(v2,r,c+1,fcount,sr,sc);}
      else
      [B]{cout << endl << fcount; return 6;}[/B]
      
      }
      
      void show (vector <vector<string> >  v2, int r , int c)
      {
      cout << endl;
      for(int i=0;i<r;i++)
      {
      for(int i2=0;i2<c;i2++)
      {
      cout << v2[i][i2];
      }
      cout << endl;
      }
      
      }
      Here's the output
      [img=http://img235.imagesha ck.us/img235/4074/pics1in2.th.jpg]
      Even when I put return 6 in the function, when I assign it to integer ff, for some reason it's 29941925.

      Comment

      Working...