Help with C++ program!

Ssargon

Registered
Hi!

I need some help with my simple C++ program that I created for terminal use in Mac OS X. The program is set to emulate a variable dice set (one or more dices that can be of any shape).

The problem is that I can´t get the program to use any user input commands that i send to the program at startup (as arguments). I´v tried with several conversion methods for char *[] to int that simply does not work. Please tell me how to make this example work!

Here is the code (some code does not display well):


#include stdlib.h>
#include string.h>
#include time.h>
#include iostream.h>


int Rulla(int antal, int starttal, int sluttal, int linenumbers){
if(starttal>sluttal){
cout<<"Fel! Det angivna starttalet är större än sluttalet."<<endl;
exit(EXIT_FAILURE);
}
else if(starttal==sluttal){
cout<<"Fel! Det angivna starttalet är lika stort som sluttalet."<<endl;
exit(EXIT_FAILURE);
}
else{
cout<<"Rullar tärningen "<<antal<<" gång(er)"<<endl;
int talet=0;
for(int i=0; i<antal; i++){
if (starttal<sluttal)
talet=starttal+rand()%(sluttal-starttal+1);
else if(starttal==sluttal)
talet=starttal;
else{
cout<<"Starttalet måste vara mindre än Sluttalet"<<endl;
break;
}
if(linenumbers==1)
cout<<i+1 <<": " <<talet <<endl;
else
cout<<talet <<endl;
}
}
}

int main(int argc, char *argv[]){
int antal=0, starttal=0, sluttal=0, exekveringsfas=0, linenumbers=1;
if(argc==1){
cout<<"Mata in antalet rullningar"<<endl;
cin>>antal;
cout<<"Mata in tärningens starttal"<<endl;
cin>>starttal;
cout<<"Mata in tärningens sluttal"<<endl;
cin>>sluttal;
cout<<"Ange om du vill ha radnummer eller inte; 1(på) eller 0(av)"<<endl;
cin>>linenumbers;
Rulla(antal, starttal, sluttal, linenumbers);
}
else if((argc==4)||(argc==5)){
srand(time(0));
antal=(int) argv[1];
starttal=(int) argv[2];
sluttal=(int) argv[3];
if(argc==5){
linenumbers=(int) argv[4];
if(linenumbers>1) linenumbers=0;
}
Rulla(antal, starttal, sluttal, linenumbers);
}
else{
cout<<"Tre till fyra tal krävs; -Antal tärningar- -Tärningarnas Starttal- -Tärningarnas Sluttal-, optionellt: 0/1 (0=radnummer av (1=standard))"<<endl;
}
}


You can also look at the code here: http://www.geocities.com/ssargon/rulla2.txt
 
Back
Top