How Can I Keep the Memory?

Nov 17, 2017 at 6:13pm
Ok so in the code down below I have made a program, the problem is that once I reset the program I need to keep the area's that have already been input. Is there a way to do this?

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
60
61
62
63
64
65
66
  #include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main(int argc, char* argv)
{

string shape;
int chosenShape;
double area;
double radius;
double radiusSq;
double length;
double width;
double side;
double base;
double height;

cout << "CARPET INSTILATION QUOTE" << endl;
restart: //restart
cout << "Shape:    ";
cin >> shape;

if (shape == "SQUARE") //SQUARE
{ cout << "Side:     ";
  cin >> side;
area= side*side;
  cout << "Area:     " << area << endl;
}else if (shape == "RECTANGLE") //RECTANGLE
{ cout << "Length:   ";
  cin >> length;
  cout << "Width:    ";
  cin >> width;
area= length*width;
  cout << "Area:     " << area << endl;
}else if (shape == "CIRCLE") //CIRCLE
{ cout << "Radius:   ";
  cin >> radius;
radiusSq= radius*radius;
area= 3.14*radiusSq;
  cout << "Area:     " << area << endl;
}else if (shape == "TRIANGLE") //TRIANGLE
{ cout << "Base:     ";
  cin >> base;
  cout << "Height:   ";
  cin >> height;
area= height*base/2;
  cout << "Area:     " << area << endl;
}else if (shape == "DONE") //DONE (STILL NEED TO DO)
{  

system("pause");
return 0;

}else

cout << "That is not a correct output";

cout << endl;
goto restart;    //restart

system("pause");
return 0;
}
Nov 17, 2017 at 6:20pm
The simplest solution would be to write the information to a text file. If they already inputted the number then write it to the file if not write a -1 or something similar in that place and next time you run the program you need to read the numbers back in and where theirs a -1 you need to still get that info.
Nov 17, 2017 at 8:52pm
I am using this program to send it to my teacher, I don’t think that putting it in a file would work
Nov 17, 2017 at 10:58pm
What do you mean by "once I reset the program"?

If the program finishes, the data is gone. If you're not happy creating a file to store the data, you don't have many other options. Pretty much no other options; anything else would just be a variation on storing data elsewhere.
Nov 17, 2017 at 11:18pm
I viewed your question the same way Repeater did. That by "once I reset the program" you meant that the program closed and you launched the application again. After further inspection, I see that you are using a goto to "restart' your program, which I believe is what you are referring to. First off, NO! Do NOT use a goto, it is bad programming practice. Use a loop! Next, ok I'm confused what you are trying to do, why do you need to keep data between runs? And in fact you currently are, just when you get new user input you replace the old value. So, what are you trying to do with saving the data between runs?
Nov 18, 2017 at 2:40am
Yes! Joe that is what I was saying, and that is the question! How am I able to save the area that has already been imputed!
Nov 18, 2017 at 2:51am
If you are not closing the application then you can store it in any simple data structure I don't know what you have learned yet but you could easily just create a vector to store the values.
Nov 18, 2017 at 6:19pm
I have not learned to do vectors, do you have a suggestion on how to redo the program to make it easier using basic c++? (I don’t mind re doing all the code)
Nov 18, 2017 at 7:20pm
Without using some sort of data structure there is not really any good way. You could create another group of variables to store whatever the last value was that one of your main variables held but this is very inefficient and would only store the last value used. If you haven't learned vectors yet, have you learned how to use arrays?
Nov 19, 2017 at 1:05am
Yeah I know how to use arrays, after further reading of the instructions I only had to make it to where they can input 2 shapes, so I am good Thanks for helping me!
Topic archived. No new replies allowed.