Help making simple C++ adding test score program

Started by
47 comments, last by HTML 20 years, 6 months ago
Ah ok, well that can be done very similar to what I previously said. Basically you want to use an array. Instead of making variables named test1,test2, test, etc.. make an array (which you can think of as a collection of variables). So your array is named test, test1 would be test[1], test2 would be test[2] and so on. Arrays have to be a fixed size, so there is a limit on how many test the user can enter. I'll write some code out for you to demonstrate how this would work.

/*We'll make an array of size 20This means the user can enter a maximum of 20 test scores beforethe array runs out of spaceIt's important to note that the subscript of an array actuallystarts at 0, so an array of size 20 goes from ArrayName[0] toArrayName[19] (20 elements)*/int test[20];int choice = 0; /*number of test scores to enter, shouldn't be greater than array size*/int i = 0;cout << "Enter number of test";cin >> choice;/*Next, you'll have to use a loop to run through the code a certain number of times*/while(i < choice){cout << "Please enter the test scores";cin >> test[i];++i; //increase i by 1 every loop}


It's important to note than you can only enter one test score
at a time with this method and it will display "Please enter the
test scores" each loop. You'll also have to modify the rest of
your code to read from an array instead of a normal variable.

Hopefully this helps.

[edited by - suikio on October 17, 2003 8:00:04 PM]
Advertisement
Thanks, that works perfectly
if you use that last example your going to need to put some overflow bounds checking... otherwise if the user inputs more then 20 tests they will overflow the array causing problems... so you might want to put a check in there to make sure they dont put in more then the arrary size...


RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios
[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Gamedev book''s]
[Virtually Online-Books][Mage Tower Ent-My Site]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

Yeah I mentioned that it my comments, should have just added another line and done it though.
hehe sorry just scanned over comments just looked at code... i shoulda scanned more thourghly.


RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios
[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Gamedev book''s]
[Virtually Online-Books][Mage Tower Ent-My Site]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

Can I do this by:

test = sizeof()

?
Not sur what to put in the () though.... Either choice or int
just put this before the while statement...

its very basic but it works

if(choice > 20){std::cout << "Error: Choice Excedes limit of 20: " << std::endl; //or somethng to that extentstd::cout << "Enter Again: ";std:: cin >> choice;}



RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios
[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Gamedev book''s]
[Virtually Online-Books][Mage Tower Ent-My Site]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

You could also set choice to 20 if it''s greater than 20, because 20 is all they can enter anyways.
if (choice > 20)
choice = 20;

true. that works but you would wnat to tell the user it did that ... so they know and dont try to post over that amount.


RanBlade
"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...Passion is where great games come from, if you dont live and breathe games you shouldn''t be in the games industry." - Dave Pottinger, Ensemble Studios
[GameDev][C++ Page][Game Tutorials][FreeBSD][HawkNL(Hawk Network Library)][NeHe Productions][Gamedev book''s]
[Virtually Online-Books][Mage Tower Ent-My Site]

Eric Ranaldi a.k.a RanBlade


[size=1]"Passion is what drives you to stay up until 4am fixing that bug that hardly anyone would notice...


[size=1]Passion is where great games come from, if you dont live and breathe games you shouldn't be in the games industry."


[size=2]- Dave Pottinger, Ensemble Studios



[size=1][GameDev][C++ Page][Unity Game Engine][Panda3D Game Engine][NeHe Productions][Drunken Hyena][MSDN][Beej's Guide to Network Programming]


[size=1][FreedBSD][My Site][Gamasutra][Khan Acadamey]

This topic is closed to new replies.

Advertisement