How do I store the multiple values in one variable, Or in other words, how do i put multiple calculate values in a single variable? Is it possible if I use arrays?(Can anyone show me an example codes).
I have to create a program where it will use class and
1. Add sphere(if I put Radius, it will calculate the volume and surface)
2. Sort added sphere by its volume size.
3. Display All
4. Quit (I know how to do this!)
Any better suggestion how to program this code :/?
You have the condition of your while loop as !done, so switch done to true in order for it to evaluate false in the next test of the loop. If you ever want to exit main without reading any lines of code that follow the end of your code, just put return 0;. The program reads it as the end of main and will exit the program normally.
To answer your question, you could use arrays to store the data, but I don't know for sure how to do it with classes. I know if you use 3 value returning functions, then you will be able to put the data into arrays. I think you could also try a void function that you pass the arrays to and it will change them as it gets to them. If you want it to be in the one object, then I would try declaring the class variables as arrays and doing a display function.
If you want to be able to change the array size, then you'll have to learn about vectors, or declare the arrays to be a lot bigger than they will ever need to be.
arrays, structs, and classes are how you would store the data. i would suggest vectors because they are resizable(arrays also are in a way but its a harder concept).
....Sphere value\main.cpp||In function 'int main()':|
....Sphere value\main.cpp|20|error: a function-definition is not allowed here before '{' token|
....Sphere value\main.cpp|57|error: expected '}' at end of input|
||=== Build finished: 2 errors, 0 warnings ===|
^ still getting an error(Can't fix it and don't know how :( I am still a newbie in programming!)
I have to use bubble sort method to sort size of the volumes.
Anyone know any good website to learn vectors?
#include <iostream>
#include <vector>
#include <algorithm>
#include <time.h>
class card{
public:
char type;
int dice, total;
};
int randomnumber();
char randomletter();
void print(std::vector<card>);
int convert(char a);
bool sortorder(const card &a, const card &b);
int main(){
std::vector<card> cards; cards.resize(5);
srand((unsigned)time(0)); bool run = true; char eord;
while(run){
start:
std::cout << "\nWould you like some cards?(Y/N)";
std::cin >> eord;
if(tolower(eord) == 'n') break;
//*******************************Numbers********************************************
//makes dice rolls
for(int i = 0; i < cards.size(); i++){ cards[i].dice = randomnumber();}
//makes card types
for(int i = 0; i < cards.size(); i++){cards[i].type = randomletter();}
print(cards);
std::cout << "\nWould you like to sort your cards?(Y/N)";
std::cin >> eord;
if(tolower(eord) == 'n') goto start;//do not really like this
//********************************Sort**********************************************
//sorts dice rolls
for(int i = 0; i < cards.size();i++){ cards[i].total = cards[i].dice+convert(cards[i].type);}
std::sort(cards.begin(), cards.end(), sortorder);
print(cards);
}
//********************************End***********************************************
return 0;
}
bool sortorder(const card &a, const card &b){
if(a.type < b.type)
returntrue;
if(a.type == b.type){
if(a.dice > b.dice)
returntrue;
}
returnfalse;
}
int convert(char a){
a = tolower(a);
if(a == 'd') return 30;
if(a == 's') return 0;
if(a == 'h') return 15;
elsereturn NULL;
}
void print(std::vector<card> card){
for(int i = 0; i < card.size(); i++) std::cout << card[i].dice << " ";
std::cout << '\n';
for(int i = 0; i < card.size(); i++) std::cout << card[i].type << " ";
}
int randomnumber(){
return rand()%14+2;
}
char randomletter(){
switch (rand()%3) {
case 0:
return'd';
break;
case 1:
return's';
break;
case 2:
return'h';
break;
default:
break;
}
return NULL;
}
random file i found on my computer. also there is a sort function in the algorithm library which you do sort(vector.begin(),vector.end());
the template for the function is void sort ( RandomAccessIterator first, RandomAccessIterator last );