how do i put a number in so it doesn't take all of the array?

hi i am making this calculator which should save 10 latest calculations in an array. but the problem is when i put a number in it just fills up the entire array, i would like it like this:

gemt[0] = first calculation;
gemt[1] = second calculation;

and so on. what i have done now doesn't work, but i must admit that i have no idea to make it.

my function looks like this:
int valgt = 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

LVL 1
smurf_killerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
Well, you're looping over the entire array, and you're setting all elements in the array to resultat :

                 for(int i=0; i<10; i++)
                 {
                         gemt[i] = resultat;
                 }

(btw, note that I left out the if ... it is of no use because the for loop already checks that condition).
0
evilrixSenior Software Engineer (Avast)Commented:
You are looping through numbers 0 - 9 and putting resultant in every position in gemt using this loop.
0
Infinity08Commented:
What you want is something like this :


for (int i = 0; i < 10; ++i) {
    float resultat = /* calculate the current result */;
    gemt[i] = resultat;
}

Open in new window

0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

smurf_killerAuthor Commented:
sorry, i forgot to write that i already had found the error, i just have no clue how to make it so i takes 1 spot at a time
0
evilrixSenior Software Engineer (Avast)Commented:
Maybe you mean...
	gemt[i++] = resultat; // Where i is the index you want to add resultant too

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
Unfortunately, there isn't enough in the code snippet provided to give you the correct answer, we can only conjecture based upon what we can see.
0
Infinity08Commented:
>> sorry, i forgot to write that i already had found the error, i just have no clue how to make it so i takes 1 spot at a time

As I showed : the calculation for the new value has to be INSIDE the for loop. Or, looking at it differently : the for loop has to be moved around the calculation.
0
evilrixSenior Software Engineer (Avast)Commented:
Also, the following won't work...

                   for(int i = 0; i<10; i++)
                     {
                             cout<<gemt[i];
                             cout.flush();
                     }


Since it's not within scope of your for loop.
0
evilrixSenior Software Engineer (Avast)Commented:
^^ ignore that, I can;'t read :)
0
smurf_killerAuthor Commented:
oh didn't think you needed anymore, ill post it here and then look at your code. my functions and array is defined in a header file, i know this isn't the best way to do it, but it works for me ;)
#include "regne.h"
#include <iostream>
 
using namespace std;
 
 
int main() {
    int valg = 0;
    float valgt = 0;
    float tal_1 = 0;
    float tal_2 = 0;
    int i = 0;
    int ab = 0;
    cout<<valgt<<endl;
 
    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; 
    valgt = static_cast<float>(valg);
    
    if(valg == 1){//plus
   
    cout<< "indtast venligst nummer 1: "; cin>>tal_1;
    cout<< "indtast venligst nummer 2: "; cin>>tal_2;
         _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;         
         _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;
        _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;         
         _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;
      _gemme(minus_1);
      cout<<"gemt"<<endl<<endl;
}
 
float _gange(float &a, float &b)
{
      float gange_1;
      gange_1 = a*b;
      cout<<a<<"*"<<b<<"="<<gange_1<<endl<<endl;
      _gemme(gange_1);
      cout<<"gemt"<<endl<<endl;
}
 
float _dividere(float &a, float &b)
{
      float div_1;
      div_1 = a/b;
      cout<<a<<"/"<<b<<"="<<div_1<<endl<<endl;
      _gemme(div_1);
      cout<<"gemt"<<endl<<endl;
}
 
float _gemme (float &resultat)
{   
 
      
  int valgt = 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

0
Infinity08Commented:
btw, smurf_killer, in your other thread, I asked you to post the exact assignment ... You didn't do that. It would help us a lot in figuring out what you need to do, and help you better.
0
evilrixSenior Software Engineer (Avast)Commented:
None of your calculation functions return anything -- still
0
Infinity08Commented:
What do you want the _gemme function to do ? Right now, it just either fills the entire array with the value 5.0, or it shows that array.
0
smurf_killerAuthor Commented:
sorry infinity, haven't seen that if it was in my previous Q, but here it goes:

epand the calculator program from chapter 4 with a function to save 10 calculated results.

.as of now, i want _gemme to save the calculations made and then post them when u enter "5" in the menu.
as it is now, it fills the array with 1 calculation, not 5 though, so that works good enough, i just need a way to only make 1 calculation fill up 1 array spot,

evilrix: does my functions have to return something?
0
evilrixSenior Software Engineer (Avast)Commented:
>> does my functions have to return something?
Yes, it needs to return a float -- that's what the function definition says it returns.

Are you trying to do something like below?
float _gemme (float &resultat, int i, int * gemt)
{   
	if(i!=5)
	{
				gemt[i] = resultat; // Use index passed in
	}
	else
	{
		for(int n = 0; n<i; n++)
		{
			cout<<gemt[n];
			cout.flush();
		}
	}
}

Open in new window

0
Infinity08Commented:
In your gemme function, you'll have to keep track of the current index. A static int would be nice for that.

Then when the function is called, it simply uses that static index to modify the element in the array with the given value, and then increments the index (not higher than 9 of course !!).
0
evilrixSenior Software Engineer (Avast)Commented:
>> In your gemme function, you'll have to keep track of the current index. A static int would be nice for that.
Or passed in from main would work. There is already an existing i variable, which could be used with a few mods (eg. it's starting value is 1 and not 0).

If you don't want your functions to return change them from like this...

float _dividere(float &a, float &b)

TO like this...

void _dividere(float &a, float &b)

Also, avoid leading underscore as it is reserved for use by the compiler and using it is invalid C++.
0
evilrixSenior Software Engineer (Avast)Commented:
>> it's starting value is 1
Again, I can't read [for(int i = 0; i<12; i++)]
0
Infinity08Commented:
>> Or passed in from main would work. There is already an existing i variable, which could be used with a few mods (eg. it's starting value is 1 and not 0).

Your way is easier. So, let's work with that.

(I had something else in mind ... A circular buffer of the last 10 calculation results ;) )
0
smurf_killerAuthor Commented:
evilrix, ill change it to void, and i will also delete the leadin underscore. but i now absolutely nothing about pointer, and i do not understand your example, like i don't know where "i" is getting its value from and therefor how you are using it.
0
evilrixSenior Software Engineer (Avast)Commented:
Ok, I've modified you code a bit to work in the way I think it possibly should. I can't test it because I don't understand the non-English in it :)

I would have preferred not to make these mods and to try and guide you (I'm sure I8 will agree and slap me for chaning your code, but hopefully this will help and not hinder your learning process) but I think there is a lot going on here and that might just get even more confusing. Can you try this and see if it kind of does what you are after and if so then we can discuss what I did and why so as to ensure you understand properly. If not then tell me what's wring and we can set about trying to rectify the issues. At least, if nothing else, you'll have code that will build.

There is no rush to close this Q, we'll stay with it until you get it.

BTW: I've don't nothing to try and rationalize your code or to fix any defects, all I've done it make it build with some kind of sensible behavior (I think).
//#include "regne.h"
#include <iostream>
 
using namespace std;
 
void _gemme (float resultat, float *gemt, int i)
{   
	int valgt = static_cast<int>(resultat);       
 
	if(valgt!=5)
	{
		gemt[i] = resultat;
	}
	else
	{
		for(int n = 0; n<i; n++)
		{
			cout<<gemt[i];
			cout.flush();
		}
	}
}
 
float  _plus(float &a, float &b)
{
	float plus_1;
	plus_1 = a+b;
	cout<<a<<"+"<<b<<"="<<plus_1<<endl;
	cout<<"gemt"<<endl<<endl;
	return plus_1;
}
 
float  _minus(float &a, float &b)
{
	float minus_1;
	minus_1 = a-b;
	cout<<a<<"-"<<b<<"="<<minus_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
 
	return minus_1;
}
 
float  _gange(float &a, float &b)
{
	float gange_1;
	gange_1 = a*b;
	cout<<a<<"*"<<b<<"="<<gange_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
	return gange_1;
}
 
float  _dividere(float &a, float &b)
{
	float div_1;
	div_1 = a/b;
	cout<<a<<"/"<<b<<"="<<div_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
 
	return div_1;
}
 
int main() {
	int valg = 0;
	float valgt = 0;
	float tal_1 = 0;
	float tal_2 = 0;
	int i = 0;
	int ab = 0;
	float gemt[12];
	cout<<valgt<<endl;
 
	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; 
		valgt = static_cast<float>(valg);
 
		if(valg == 1){//plus
 
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			_gemme(_plus(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 2){//minus
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			_gemme(_minus(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 3){//gange
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			_gemme(_gange(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 4){//dividere
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			_gemme(_dividere(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 5){
			_gemme(valgt, gemt, i);    
		}
		else {//lukke
			return 0;
		}
	}
 
 
 
	cin.get();
	cin.get();
	return 0;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
BTW: What language is the text, German? I have no idea what is says :)
0
Infinity08Commented:
Just a small fix in evilrix's code :

                  cout<<gemt[i];

should be :

                  cout<<gemt[n];

in the gemme function.


>> BTW: What language is the text, German? I have no idea what is says :)

Looks like Norwegian, or Swedish or thereabouts.
0
evilrixSenior Software Engineer (Avast)Commented:
Thanks for that I8 -- just spotted it myself when you posted :)

void _gemme (float resultat, float *gemt, int i)
{   
	int valgt = static_cast<int>(resultat);       
 
	if(valgt!=5)
	{
		gemt[i] = resultat;
	}
	else
	{
		for(int n = 0; n<i; n++)
		{
			cout<<gemt[n] << std::endl;
		}
	}
}

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
>> Just a small fix in evilrix's code
BTW: I hope you both agree with my decision to publish modified code even though this is an academic Q. I obviously did it to try and circumvent confusion so we have something to work with  and not to try and provide a quick answer.
0
Infinity08Commented:
As long as smurf_killer sticks with us, so we can complete the assignment together, and as long as smurf_killer understands everything, it's fine by me, evilrix.


Btw, there's a bit of a problem with the gemme function ... If the result of one of the calculations happens to be 5, then it won't be stored, but the array will be shown. Maybe you should have two separate functions, one for printing, and one for adding values. Or maybe you should add another parameter to the function.
0
evilrixSenior Software Engineer (Avast)Commented:
>> everything, it's fine by me, evilrix.
Cool. I'm glad we're on the same page. Anyway, I have to go away for some time now anyway so I'll leave it with you I8. I'll pick it up later if the thread's still open.

Good luck both of you :)

-Rx.
0
smurf_killerAuthor Commented:
thanks guys, much appreciated, ill look deeper into the code now. i have thought of using a second function, but i was not sure how to link to that. and i don't think that you are breaking any rules as this is just a private study with me, my book and my computer :D but ill take a deeper look into it
0
Infinity08Commented:
You can keep this question open until the code is working as you want it.

If you have questions, do not hesitate to ask ... We're here to help, but we can't know what you have a problem with unless you tell us ;)


>> i have thought of using a second function, but i was not sure how to link to that.

The gemme function almost always needs to modify a value. There is just one specific case where it needs to output the array, and that's when the user enters 5. That's how you can easily make the distinction between the two functions. In almost all cases, the first function is called (the one that updates the array), except in the case of 5, where the second function is called (the one that displays the array).
0
smurf_killerAuthor Commented:
well i have a question on the gemme. but i found out something, when you enter a value, then presses 5 to see it and then enters a value, it gives some weird numbers like if you hadn't set the variable to 0 if you know what i mean. anyway ill comment the  lines:
void _gemme (float resultat, float *gemt, int i) //how do you get the results into "resultat. and also as said before i have no clue about pointers so im not sure about "float *gemt". also im not absolutely sure about the int i but ill get to that
{   
	int valgt = static_cast<int>(resultat); 
 
	if(valgt!=5)
	{
		gemt[i] = resultat;  //where does it get the "i" from?
	}
	else
	{
		for(int n = 0; n<i; n++)
		{
			cout<<gemt[n]<<" ";
			cout.flush();
		}
	}
}    
 
//and also, you are returning all the other functions, wouldn't it be better to make them void, or wouldn't they be able to handle a float?

Open in new window

0
smurf_killerAuthor Commented:
oh and btw its in danish so you were almost right infinity :D
0
Infinity08Commented:
>> //how do you get the results into "resultat. and also as said before i have no clue about pointers so im not sure about "float *gemt". also im not absolutely sure about the int i but ill get to that

resultat is a function parameter. You set the value by passing a value to the function when calling it :

        gemme(10.0f, gemt, i);

will set resultat to 10.0f, will set the gemt pointer to make it point to the gemt array in main, and will set i to the i value from main.

Inside gemt, we can now use these values resultat, gemt and i :

        void gemme(float resultat, float *gemt, int i) {
             // here resultat will have the value 10.0f
             // and gemt will point to the gemt array in main
             // and i will have the value that i had in main just before this function was called
        }

Take a look at this nice tutorial about functions :

        http://www.cplusplus.com/doc/tutorial/functions.html

It will clear up a lot ...
0
smurf_killerAuthor Commented:
i think i understand it now
0
smurf_killerAuthor Commented:
but is it okay if i leave it open just a little more?
0
evilrixSenior Software Engineer (Avast)Commented:
Of course.
0
evilrixSenior Software Engineer (Avast)Commented:
>> and also, you are returning all the other functions, wouldn't it be better to make them void, or wouldn't they be able to handle a float?
Do you refer the calculation functions? It make more sense for them just to return their result and for you to then do something with it. Trying to make a function do too much usually leads to hard to maintain design and hard to fathom out code. A function should ideally do one thing and one thing only (the clue is in the name, the word function is singular and not plural). Let these functions return their result and then let the caller decide what should be done with it.
0
smurf_killerAuthor Commented:
ok so ideally they should just return the end value and i print it in the main?
0
smurf_killerAuthor Commented:
when i enter a value, then presses 5 to see it and then enters calculates something again, and look at it again, it gives some weird numbers like if you hadn't set the variable to 0 if you know what i mean.
0
evilrixSenior Software Engineer (Avast)Commented:
>> ok so ideally they should just return the end value and i print it in the main?
Maybe, but that's not essential -- passing the value to a printing function that does some formatting would be fine too.

>> when i enter a value, then presses 5 to see it and then enters calculates something again, and look at it again, it gives some weird numbers like if you hadn't set the variable to 0 if you know what i mean.
Please post all your code and example output.
0
smurf_killerAuthor Commented:
an example, first "2-2=0; 5+5=10;" then i look at it and it says 0 10. and thats fine i can do the formatting later. but then if i say "1+1=2" it says "0 10 7.7936e+033 2" and if i keep asking for it to print it out the numbers just increase, i.e. more blocks like " 0 10 7.7936e+033 2 7.86004e+033" and so on

#include <iostream>
 
using namespace std;
 
void _gemme (float resultat, float *gemt, int i)
{   
	int valgt = static_cast<int>(resultat);       
 
	if(valgt!=5)
	{
		gemt[i] = resultat;
	}
	else
	{
		for(int n = 0; n<i; n++)
		{
			cout<<gemt[n]<<" ";
			cout.flush();
		}
	}
}
 
float  _plus(float &a, float &b)
{
	float plus_1;
	plus_1 = a+b;
	cout<<a<<"+"<<b<<"="<<plus_1<<endl;
	cout<<"gemt"<<endl<<endl;
	return plus_1;
}
 
float  _minus(float &a, float &b)
{
	float minus_1;
	minus_1 = a-b;
	cout<<a<<"-"<<b<<"="<<minus_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
 
	return minus_1;
}
 
float  _gange(float &a, float &b)
{
	float gange_1;
	gange_1 = a*b;
	cout<<a<<"*"<<b<<"="<<gange_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
	return gange_1;
}
 
float  _dividere(float &a, float &b)
{
	float div_1;
	div_1 = a/b;
	cout<<a<<"/"<<b<<"="<<div_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
 
	return div_1;
}
 
int main() {
	int valg = 0;
	float valgt = 0;
	float tal_1 = 0;
	float tal_2 = 0;
	int i = 0;
	int ab = 0;
	float gemt[12];
	cout<<valgt<<endl;
 
	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; 
		valgt = static_cast<float>(valg);
 
		if(valg == 1){//plus
 
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			_gemme(_plus(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 2){//minus
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			_gemme(_minus(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 3){//gange
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			_gemme(_gange(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 4){//dividere
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			_gemme(_dividere(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 5){
			_gemme(valgt, gemt, i);    
		}
		else {//lukke
			return 0;
		}
	}
 
 
 
	cin.get();
	cin.get();
	return 0;
}

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
The problem is that when you select 5 to display what's in your buffer you end up incrementing i in your for loop but never add anything to the gemt buffer because at this point you are not calculating anything new to go in it. When you select 5 you don't want i to be incremented in your for loop; however, this is automatically doe as part of the for loop itself so the next best thing is to decrement i when 5 is chosen, after the call to _gemme, so that when i is incremented in the for loop its value is reset to what it was originally (eg. it starts off as 3, 5 is selected so you decrement i to 2 then the for loop repeats so its incremented back to 3). See line 104 in the modified code below.
#include <iostream>
 
using namespace std;
 
void _gemme (float resultat, float *gemt, int i)
{   
	int valgt = static_cast<int>(resultat);       
 
	if(valgt!=5)
	{
		gemt[i] = resultat;
	}
	else
	{
		for(int n = 0; n<i; n++)
		{
			cout<<gemt[n]<<" ";
			cout.flush();
		}
	}
}
 
float  _plus(float &a, float &b)
{
	float plus_1;
	plus_1 = a+b;
	cout<<a<<"+"<<b<<"="<<plus_1<<endl;
	cout<<"gemt"<<endl<<endl;
	return plus_1;
}
 
float  _minus(float &a, float &b)
{
	float minus_1;
	minus_1 = a-b;
	cout<<a<<"-"<<b<<"="<<minus_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
 
	return minus_1;
}
 
float  _gange(float &a, float &b)
{
	float gange_1;
	gange_1 = a*b;
	cout<<a<<"*"<<b<<"="<<gange_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
	return gange_1;
}
 
float  _dividere(float &a, float &b)
{
	float div_1;
	div_1 = a/b;
	cout<<a<<"/"<<b<<"="<<div_1<<endl<<endl;
	cout<<"gemt"<<endl<<endl;
 
	return div_1;
}
 
int main() {
	int valg = 0;
	float valgt = 0;
	float tal_1 = 0;
	float tal_2 = 0;
	int i = 0;
	int ab = 0;
	float gemt[12];
	cout<<valgt<<endl;
 
	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; 
		valgt = static_cast<float>(valg);
 
		if(valg == 1){//plus
 
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			_gemme(_plus(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 2){//minus
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			_gemme(_minus(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 3){//gange
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;
			_gemme(_gange(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 4){//dividere
			cout<< "indtast venligst nummer 1: "; cin>>tal_1;
			cout<< "indtast venligst nummer 2: "; cin>>tal_2;         
			_gemme(_dividere(tal_1,tal_2), gemt, i);  
		}
		else if(valg == 5){
			_gemme(valgt, gemt, i);
			--i;
 
		}
		else {//lukke
			return 0;
		}
	}
 
 
 
	cin.get();
	cin.get();
	return 0;
}

Open in new window

0
evilrixSenior Software Engineer (Avast)Commented:
BTW: This is what the C++ Standard has to say regarding leading underscores...

"17.4.3.1.2 Global names
Certain sets of names and function signatures are always reserved to the implementation:
 Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use.
 Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace"

This being the case, it is best to avoid using leading underscores.
0
smurf_killerAuthor Commented:
thanks. i will look at it tomorrow, and will probably also close the question tomorrow
0
Infinity08Commented:
>> The problem is that when you select 5 to display what's in your buffer you end up incrementing i in your for loop but never add anything to the gemt buffer because at this point you are not calculating anything new to go in it.

Heh. One other reason to use a static index inside the gemme function ;)
0
smurf_killerAuthor Commented:
thank you  guys for all the help you have given me
0
evilrixSenior Software Engineer (Avast)Commented:
You are very welcome.
0
smurf_killerAuthor Commented:
i really do hope that you all feel like i am glad for what you have done. evilrix, i tried to give you as many points as i could but i kinda ran out of them. i really hope that you are both satisfied as you have deserved them, even if it is not much
0
evilrixSenior Software Engineer (Avast)Commented:
smurf_killer,

Thank you for your kind words on the close message, they are very much appreciated. You are very welcome indeed.

Good luck my friend.

-Rx.
0
Infinity08Commented:
Glad to be of assistance ;) Don't hesitate to ask in case of further questions/problems !
0
smurf_killerAuthor Commented:
there will be thats for sure :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.