Link to home
Start Free TrialLog in
Avatar of smurf_killer
smurf_killer

asked on

function isn't printing my array

hi im making a calculator and now i wan't it to save the 10 last calculations in an array, i can compile it but for some reason i won't print them out when im pressing "5" which should call that function. let me know if i should translate it or light up anything
#include "regne.h"
#include <iostream>
 
using namespace std;
 
 
 
int main() {
    int valg;
    float _valgt = (float) valg;
    float tal_1 = 0;
    float tal_2 = 0;
    float resultat = 0;
    int i = 0;
    int ab = 0;
    
 
    cout<< "velkommen til lommeregneren, hvad kunne du tænke dig at gøre?"<<endl<<endl;
 
 
    
    for(int i = 0; i<12; i++)
    {
    cout<< "for plus tryk 1, for minus tryk 2, for gange tryk 3, for dividere tryk 4. Hvis du vil se det gemte tryk 5, eller luk ved at trykke 6"<<endl;
    cin>>valg; 
    
    if(valg == 1){//plus
   
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;
         resultat = _plus(tal_1,tal_2);
        }
    else if(valg == 2){//minus
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
         resultat = _minus(tal_1,tal_2);
         }
    else if(valg == 3){//gange
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;
         resultat = _gange(tal_1,tal_2);
         }
    else if(valg == 4){//dividere
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
         resultat = _dividere(tal_1,tal_2);
         }
    else if(valg == 5){
    _gemme(_valgt);    
}
    else {//lukke
         return 0;
         }
         }
 
 
 
cin.get();
cin.get();
return 0;
}
    
float _plus(float &a, float &b)
{
      float plus_1;
      plus_1 = a+b;
      cout<<a<<"+"<<b<<"="<<plus_1<<endl;
      _gemme(plus_1);
      cout<<"gemt"<<endl<<endl;
}
 
float _minus(float &a, float &b)
{
      float minus_1;
      minus_1 = a-b;
      cout<<a<<"-"<<b<<"="<<minus_1<<endl<<endl;
}
 
float _gange(float &a, float &b)
{
      float gange_1;
      gange_1 = a*b;
      cout<<a<<"*"<<b<<"="<<gange_1<<endl<<endl;
}
 
float _dividere(float &a, float &b)
{
      float div_1;
      div_1 = a/b;
      cout<<a<<"/"<<b<<"="<<div_1<<endl<<endl;
}
 
float _gemme (float &resultat)
{   
    int valg;
    resultat = valg;  
               
      
      if(valg!=5)
      {
                 for(int i=0; i<10; i++)
                 {
                         if(i<10)
                         {
                                 gemt[i] = resultat;
                         }
                         else
                         {
                             cout<<"kan ikke gemme flere resultater"<<endl;
                         }
                 }
                 }
                 else
                 {
                     for(int i = 0; i<10; i++)
                     {
                             cout<<gemt[i];
                     }
                 
      
}
}
 
 
 
//and this is my headerfile "regne.h"
 
float _plus (float &a, float &b);
float _minus(float &a, float &b);
float _gange(float &a, float &b);
float _dividere(float &a, float &b);
float _gemme (float &resultat);
float gemt[10];

Open in new window

Avatar of cup
cup

Try cout.flush () after the cout << statement where you want the number to display.
Avatar of smurf_killer

ASKER

it still wont show it :S
Hello
You have declared a local variable valg in the function _gemme. The if test the uninitalized value of the local variable. You have to declare the variable valg at file scope (at the top of the file outside the main function) to use it, or use another parameter to the _gemme function with value of valg.
thanks that sounds reasonable, ill try it and get back
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You have to always keep in mind the scope of variables. When you declare a variable, its scope will be until the end of the enclosing block. That variable will NOT be visible anywhere else in the code, including other functions.

For example :

        void fun() {
            // val is NOT visible here !!!
        }

        void fun2() {
            int val;        // <--- the scope of val is the fun2 function
            fun();
        }
sorry for the late answer but my internet was down. ive tried what you said but it still wont work, this is my code now.
#include "regne.h"
#include <iostream>
 
using namespace std;
 
 
int valg;
float valgt = static_cast<float>(valg);
int main() {
    
    float tal_1 = 0;
    float tal_2 = 0;
    float resultat = 0;
    int i = 0;
    int ab = 0;
    
 
    cout<< "velkommen til lommeregneren, hvad kunne du tænke dig at gøre?"<<endl<<endl;
 
 
    
    for(int i = 0; i<12; i++)
    {
    cout<< "for plus tryk 1, for minus tryk 2, for gange tryk 3, for dividere tryk 4. Hvis du vil se det gemte tryk 5, eller luk ved at trykke 6"<<endl;
    cin>>valg; 
    
    if(valg == 1){//plus
   
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;
         resultat = _plus(tal_1,tal_2);
        }
    else if(valg == 2){//minus
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
         resultat = _minus(tal_1,tal_2);
         }
    else if(valg == 3){//gange
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;
         resultat = _gange(tal_1,tal_2);
         }
    else if(valg == 4){//dividere
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
         resultat = _dividere(tal_1,tal_2);
         }
    else if(valg == 5){
    _gemme(valgt);    
}
    else {//lukke
         return 0;
         }
         }
 
 
 
cin.get();
cin.get();
return 0;
}
    
float _plus(float &a, float &b)
{
      float plus_1;
      plus_1 = a+b;
      cout<<a<<"+"<<b<<"="<<plus_1<<endl;
      _gemme(plus_1);
      cout<<"gemt"<<endl<<endl;
}
 
float _minus(float &a, float &b)
{
      float minus_1;
      minus_1 = a-b;
      cout<<a<<"-"<<b<<"="<<minus_1<<endl<<endl;
}
 
float _gange(float &a, float &b)
{
      float gange_1;
      gange_1 = a*b;
      cout<<a<<"*"<<b<<"="<<gange_1<<endl<<endl;
}
 
float _dividere(float &a, float &b)
{
      float div_1;
      div_1 = a/b;
      cout<<a<<"/"<<b<<"="<<div_1<<endl<<endl;
}
 
float _gemme (float &resultat)
{   
 
      
               
      
      if(valgt!=5)
      {
                 for(int i=0; i<10; i++)
                 {
                         if(i<10)
                         {
                                 gemt[i] = resultat;
                         }
                         else
                         {
                             cout<<"kan ikke gemme flere resultater"<<endl;
                         }
                 }
                 }
                 else
                 {
                     for(int i = 0; i<10; i++)
                     {
                             cout<<gemt[i];
                             cout.flush();
                     }
                 
      
}
}

Open in new window

>> ive tried what you said but it still wont work
It would help if you provided more information about what doesn't work.

Did you review the comments I made inline in your code? You seemed to have overlooked some of them. Again, I've added inline comments to your code. Can I suggest you read through them carefully and any you don't understand feel free to ask for additional info here?

-Rx.
#include "regne.h"
#include <iostream>
 
using namespace std;
 
 
// RX: <--- [ You still have unnecesary file scoped variables ]
int valg;
float valgt = static_cast<float>(valg); // RX: <--- [ Why not just initialize this with 0.0? BTW: globals are always set to 0 by default ]
 
int main() {
 
	float tal_1 = 0;
	float tal_2 = 0;
	float resultat = 0;
	int i = 0;
	int ab = 0;
 
 
	cout<< "velkommen til lommeregneren, hvad kunne du tænke dig at gøre?"<<endl<<endl;
 
 
 
	for(int i = 0; i<12; i++)
	{
		cout<< "for plus tryk 1, for minus tryk 2, for gange tryk 3, for dividere tryk 4. Hvis du vil se det gemte tryk 5, eller luk ved at trykke 6"<<endl;
		cin>>valg; 
 
		if(valg == 1){//plus // RX: <--- [ Use a switch/case, it'll be simpler ]
 
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			resultat = _plus(tal_1,tal_2); // RX: <--- [ You don't do anything with resultant ]
		}
		else if(valg == 2){//minus
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			resultat = _minus(tal_1,tal_2); // RX: <--- [ You don't do anything with resultant ]
		}
		else if(valg == 3){//gange
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			resultat = _gange(tal_1,tal_2); // RX: <--- [ You don't do anything with resultant ]
		}
		else if(valg == 4){//dividere
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			resultat = _dividere(tal_1,tal_2); // RX: <--- [ You don't do anything with resultant ]
		}
		else if(valg == 5){
			_gemme(valgt);    
		}
		else {//lukke
			return 0;
		}
	}
 
 
 
	cin.get();
	cin.get();
	return 0;
}
 
float _plus(float &a, float &b)
{
	float plus_1; // RX: <--- [ Why not initialize it when you define it? ]
	plus_1 = a+b;
	cout<<a<<"+"<<b<<"="<<plus_1<<endl;
	_gemme(plus_1);
	cout<<"gemt"<<endl<<endl;
 
	// RX: <--- [ Return value? ]
}
 
float _minus(float &a, float &b)
{
	float minus_1; // RX: <--- [ Why not initialize it when you define it? ]
	minus_1 = a-b;
	cout<<a<<"-"<<b<<"="<<minus_1<<endl<<endl;
 
	// RX: <--- [ Return value? ]
}
 
float _gange(float &a, float &b)
{
	float gange_1; // RX: <--- [ Why not initialize it when you define it? ]
	gange_1 = a*b;
	cout<<a<<"*"<<b<<"="<<gange_1<<endl<<endl;
 
	// RX: <--- [ Return value? ]
}
 
float _dividere(float &a, float &b)
{
	float div_1; // RX: <--- [ Why not initialize it when you define it? ]
	div_1 = a/b;
	cout<<a<<"/"<<b<<"="<<div_1<<endl<<endl;
 
	// RX: <--- [ Return value? ]
}
 
float _gemme (float &resultat)
{   
	if(valgt!=5) // RX: <--- [ This should have been passed into the function, not gloabl ]
	{
		for(int i=0; i<10; i++)
		{
			if(i<10)
			{
				gemt[i] = resultat; // RX: <--- [ Where is gemt defined? ]
			}
			else
			{
				cout<<"kan ikke gemme flere resultater"<<endl;
			}
		}
	}
	else
	{
		for(int i = 0; i<10; i++)
		{
			cout<<gemt[i]; // RX: <--- [ Where is gemt defined? ]
			cout.flush();
		}
	}
 
	// RX: <--- [ Return value? ]
}

Open in new window

i have corrected those you have pointed out to me, ill fix the if() to switch later. but im rather unsure about your 2 first comments, as im not quite sure what you mean. and also, my array and my functions is all defined in a header file, thats what my book tells me and actually it works for me.

but i think ive narrowed it down to the "valgt" variable, it doesn't seem to get the value of "valg", do you have any idea why that is? i tried cout it in my function where i need it but its just 0, so that must be the reason why it never gets into the if loop. also im not quite sure why the function "_gemme" should return something as you say, is it because it isn't void?
>> but i think ive narrowed it down to the "valgt" variable, it doesn't seem to get the value of "valg",

That's because you never set the valgt variable. You just initialize it at the start of the program here :

        int valg;
        float valgt = static_cast<float>(valg);

So, it will contain the value 0.0

After that you never modify it, so for the whole duration of the program, it will have that value 0.0

As I said earlier : scope, scope and scope. Keep track of which variable you want to change where, and whether the scope of the variable allows you to do that.
>> scope, scope and scope
And keep the scope of variables to only the place you need them. Declaring global variables makes development and maintenance problematic. It would be better to define your two globals in main and then pass them to the functions that need them. Since you are using C++ you can pass them by reference to the cost is low and the semantics of use would be as though they were local variables. If your book is telling you to use global variables I'd consider getting a new book!

http://c2.com/cgi/wiki?GlobalVariablesAreBad
>> If your book is telling you to use global variables I'd consider getting a new book!

Especially if it says to define them in a header file :)
>> the semantics of use would be as though they were local variables
I should probably clarify that statement. I meant the usage semantics, ie you don't need to handle them as a pointer, rather you can use object semantics (valg= 0 ratehr than *pvalg = 0). You should note though that since they are passed by reference any changes will still be visible to all places they are referenced -- which is what you want, right?
>> Especially if it says to define them in a header file :)
Good point, as this will break the one definition rule.
http://en.wikipedia.org/wiki/One_Definition_Rule
but they are in the global scope, so in that way they should be accesible anywhere? but if i say valgt = valg; then it would just come up with an error that it can't convert a int to float?
well i tried to do it with a reference, as shown in my first code, but that didn't work either
>> but if i say valgt = valg; then it would just come up with an error that it can't convert a int to float?

        valgt = static_cast<float>(valg);
I'd question the validity of this cast though, since you could just do...

int valg = 0;
float valgt  = 0.0;
Strictly speaking it should be float valgt  = 0.0f; to indicate it's a float and not a double; however, most compilers won't complain about this (VC 2005 doesn't).
>> well i tried to do it with a reference, as shown in my first code, but that didn't work either

In your first code, you had this (kept just the relevant code) :


int main() {
    int valg;                     // <--- valg has an undefined value here
    float valgt = (float) valg;   // <--- valgt now has that same undefined value, but cast to a float
    
    cin >> valg;                  // <--- now valg contains the value given by the user
 
    if (valg == 5) {
      gemme(valgt);               // <--- valgt still has an undefined value, and that's the value passed to gemme()
    }
 
    return 0;
} 
 
 
float gemme (float &resultat) {   // <--- resultat is a reference to valgt (which still contains the undefined value)
    int valg;                     // <--- this is a local valg (different from the one in main !!!). Again it has an undefined value
    resultat = valg;              // <--- now, that new undefined value is assigned to resultat (and thus to the valgt in main)
               
    if (valg != 5) {              // <--- this will probably NOT be true, as valg has an undefined value
        // <SNIP>
    }
    else {
        // <SNIP>
    }
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Return value?
infinity, thats what ive already done, but should they be in my main() instead?

evilrix: the problem is that i need the value from valg, and as "_gemme" is a float i can't use it there as a reference. if that was what you ment though
>> infinity, thats what ive already done, but should they be in my main() instead?

No you haven't. Did you understand the comments I added to your code in my previous post ?
>> It is valid like this

My point was, I'm unclear as the whole point of mixing int and float in the way it's being done here. The only place there needs to be a conversion is in the functions that do arithmetic with the int values that could, potentially, result in a float. There seems to be an eclectic mix going on here that doesn't make much sense.

>> the problem is that i need the value from valg, and as "_gemme" is a float i can't use it there as a reference. if that was what you ment though

Why not?
>> My point was,

Yep. I agree.

smurf_killer, this is code for an assignment/exercise if I understood it well. Can you post the exact assignment ?
>> Yep. I agree.
That's good, I thought I'd overlooked something :)
now i actually got it to work thanks, the hole problem was where i had put the static cast. but now i just cout's out all 10. but ill open a new question for that if i can't find the problem

but thanks for your help all of you, much appreciated
I also suggest reading these tutorials in order to get a better understanding :

        1) variables : http://www.cplusplus.com/doc/tutorial/variables.html
                especially the paragraphs about scope and initialization

        2) functions : http://www.cplusplus.com/doc/tutorial/functions.html
                especially the part about passing arguments, and their scope
thanks ill do that
May I ask for the reason for the B grade ? That usually means that there was something missing in our answers. If so, can you explain what ? Note also that you can always ask for clarification if something requires further explanation.
Um, I'd pointed out the need to use static_cast a couple of times before the actual accepted answer! This being the case I am wondering why this wasn't at least a point split!

http:#20807289
http:#20810037
>> May I ask for the reason for the B grade ? That usually means that there was something missing in our answers.
Two experts put a lot of effort into assisting with this question and, to be honest, I don't think neither the answer selected nor the grade given reflect this. There are many things wrong with the code posted, the accepted answer only identified one of these, many others were pointed out in various posts from both experts. This being the case I have requested a moderator review this thread to provide objective clarity.
>> the hole problem was where i had put the static cast.

Did you move it after the cin >> valg; ? Like I posted in my post with id 20813380 ?

If so, then that's good, but it's only one of the problems with your code as evilrix pointed out.

This thread contains a LOT of good information that will help you fix the code. You just have to read through it all, and every time something isn't clear, ask about it.
>> If so, then that's good, but it's only one of the problems with your code as evilrix pointed out.
This was the last part of a big jigsaw, but the selected answer only reflects this last part and not the effort put into helping you solve the rest of it. Anyway, I'll let a moderator decide the closure of this Q is appropriate or not. Other than that, I'm glad you managed to get it working.

-Rx.
i was thinking alot about how to close this question, and i was very unsure, but in the end, that was the problem
also, i will admit that the points were given out incororectly, and also the grade. but in the time i thought that it was the right one. i hope that the moderator can split them out more evenly
>> but in the end, that was the problem
There were many problems, lots of which would mean the code wouldn't even have built. The fact that you modified you code in-between posts after following out advice serves to confirm that assertion. Look back through the posts, there were many issues pointed out, all of which would have prevented your code from working.

The answer you selected was just the last problem you needed to solve so of course that was the one that finally solved the problem for you but without the other issues being pointed out that wouldn't have helped you either!
Do you understand that that's not the only problem ?

Can you explain the B grade ?
sorry infinity i can't really explain it. as i said in the precious post in felt right in the moment, but now i see it was wrong. i hope a moderator maybe can correct it
>> sorry infinity i can't really explain it. as i said in the precious post in felt right in the moment, but now i see it was wrong. i hope a moderator maybe can correct it

No worries. A moderator will be happy to review and, if they deem appropriate, reopen the Q.
great, im still quite new to this community i.e. haven't used it so much so im not used to accept multiple answers
fyi :

        How do I close a question : https://www.experts-exchange.com/help.jsp#hi331
>> great, im still quite new to this community i.e. haven't used it so much so im not used to accept multiple answers
No worries. Normally I'd accept the answer selected but on this occasion I just didn't feel it did I8 or myself justice. We put a lot of effort and care into trying to provide help, all we ask is the same effort is put into closing the Q: -

a) To show appreciation for our assistance
b) To ensure anyone else who finds this Q can see what the correct answer actually was.

Unfortunately, this Q has many answers that contributed to the final solution. What I'd suggest is that you  pick the few that helped you the most in reaching a working solution (if they only turn out to be I8's posts then so be it).

As for the grade, as I8 said, it should reflect how compete you feel the answers were and how helpful you feel the experts have been in assisting you. However, you don't need to close a Q if you are not happy, you can always ask for more assistance.

Anyway, welcome to the site.
i certainly hope that you agree with the points given now, im just sorry that i couldn't give you more points
Many thanks sk.