LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-29-2008, 07:01 PM   #1
onthefritz
Member
 
Registered: Dec 2005
Location: Colorado
Distribution: Ubuntu, RHEL, openSUSE
Posts: 105

Rep: Reputation: 19
Returning a variable?


Ok, not sure why this is happening...but when I try and do basic math in C++ it never comes out correct. Here is the code I'm using.

When trying to divide 1/1 and return it I get: 1.14885e+09

There is a link to a picture of the error I'm getting HERE
Maybe someone can figure it out. If you need I will include the full code. Keep in mind this is my first program. I am self teaching myself!

Thank you

OTF

P.S. This is C++ code in the Geany IDE.

Last edited by onthefritz; 03-29-2008 at 07:02 PM.
 
Old 03-29-2008, 07:49 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Are you sure the problem isn't in SetCharStat? Could you post the code for that?
 
Old 03-29-2008, 08:10 PM   #3
onthefritz
Member
 
Registered: Dec 2005
Location: Colorado
Distribution: Ubuntu, RHEL, openSUSE
Posts: 105

Original Poster
Rep: Reputation: 19
I don't think so. It just sets the arrCharInfo array info. It prompts for it and you enter it. You can see that it passes them correctly because the cout lines within CalcSizeValue. They show the correct numbers.
Code:
float CalcSizeValue( float fltSize1, float fltSize2)
{
 float fltEndValue;
 cout<<fltSize1<<"\n";
 cout<<fltSize2<<"\n";
 fltEndValue =  ( fltSize1 / fltSize2 ) * 1000;
 cout<<"value for EndVal is "<<fltEndValue<<"\n";
return fltEndValue;
}
Here is all thAT SetCharStat does.
Code:
    case 1:
             cout<<"Please enter Character's size from 1 to 10\n";
             cin>> arrCharInfo[intCharNum][0];
             cin.ignore();
             break;
Even if I pull out the arrCharInfo variables are replace them with (1,1) I get the same issue....
It looks like it is returning the memory value....like it was a pointer....??

Last edited by onthefritz; 03-29-2008 at 08:20 PM. Reason: update
 
Old 03-29-2008, 08:28 PM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Well, I'd still need to see more of your code to pinpoint the problem, but I have calculated that the float value 1000.0f has the same bit pattern as the integer 1148846080. This is probably significant.
 
Old 03-29-2008, 09:07 PM   #5
onthefritz
Member
 
Registered: Dec 2005
Location: Colorado
Distribution: Ubuntu, RHEL, openSUSE
Posts: 105

Original Poster
Rep: Reputation: 19
Ok, this is weird. I fixed it by dropping it into it's own function and just calling the function. Not sure why it works though.

Another weird thing is that if I have SetCharStat called right next to each other like this, it only runs it one time.
Code:
    SetCharStat(1);
    SetCharStat(2);
But if I put something between them like this...it runs just fine.
Code:
    SetCharStat(1);
    cout<<"\n";
    SetCharStat(2);
Anyone know why?

OTF
 
Old 03-29-2008, 10:35 PM   #6
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Probably a buffering issue. Try flushing cout after you print to it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prompt not hard returning after PS1 variable change? SlowCoder Linux - Newbie 5 05-01-2007 10:16 PM
Replace variable with user defined variable ce124 Programming 10 04-13-2007 10:29 AM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 12:23 PM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 09:47 PM
C++: Deleting a variable before it is returned but still returning it. Possible? Orkie Programming 3 04-08-2006 03:51 AM


All times are GMT -5. The time now is 07:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration