# Can someone look over my C++ code please?

Discussion in 'Mac Programming' started by Sirena400, Feb 22, 2005.

1. ### Sirena400 macrumors newbie

Joined:
Feb 7, 2005
#1
*sighs* I got a big problem, because the other students in my class acted up for the substitute while my teacher was gone, my teacher now has forbidden us to go into the computer lab until he thinks we're ready again, so now I have to pay for the other kid's actions because we still have to do programs, but now we just have paper and pencil to work with and I don't have any help from the computer. *sighs* So I don't know whether or not this code works or not, could someone look it over and tell me whether or not it looks okay?

The problem is we have to figure out the most undo buffers ever needed in a sequence of actions. C will stand for the user changing the data set, U will stand for the user undoing the most recent change, R will stand for the user redo an action(ie. cancelling the most recent Undo), and S will stand for the user saving the data set. I gave each letter a number value that'll make it easier, C equal 1, U will equal -1, R will equal 1, and S just resets the added up value to zero again. Some examples are:
CCCCCCCC -> this would require 8 buffers because the that's the most the data was changed another harder example is:
CCCCURUUCSCUCUS so using the values i set up, the first CCCC = 4, subrtract 1 for the U which leaves the current score at 3, add 1 for the R making the current score 4 again, minus 2 for the next two U's making the current = 2, add 1 for the C which makes current = 3, then S resets that to 0, and the next set of CUCU cancels each other out and the S at the end resets it to 0. so throughout that sequence, the highest number of times the data was changed was 4 times(right at the beggining) so the session requires 4 undo buffers because that was the most that was changed. hopefully you guys get what i'm saying ^^;

so here's what I have so far:

#include<iostream.h>
int main()
{
char letter;
cin>>letter;
int current=0;
int max=0;
switch(letter)
{
case 'C': current+=1;
break;
case 'U': current-=1;
break;
case 'R': current+=1;
break;
default:
break;
}
If current>max
then current=current
If current<max
then current=max
If current=max
then current=current

cout<<"This session requires "current" undo buffers.";
return 0;
}

I have no idea if this is right, but this is what I have so far. Also, I can't figure out how to set it up that max equals the highest number that was added up during the sequence of action and how to set it so the current is reset to 0 when S appears. Any and all help would be very greatly appreciated! Thanks!

2. ### PredDawg8 macrumors newbie

Joined:
Feb 16, 2005
#2
Well, you're sorta on the right track, but I see several errors.

First, are these letters input from the keyboard? I'm assuming so, it seems a little early for you to be doing file input.

OK, on to the errors. You need a loop. As is, your program will read one letter, determine what that letter is, add or subtract from current, and then print out either -1, 0, or 1.

Adding the reset is rather simple. Add a case to your switch for 'S'. In that case, you set current = 0.

Your if... else with the current and max is incorrect. I'll go ahead and tell you it would be this:

Code:
```if(current > max)
max = current;```
That's all you need for that.

Also, you need some input letter to indicate that the session is over and you can exit the loop.