-
What did I do wrong?
I am new to programming with C++. I started to make a program that is eventually meant to be a calculator for an online game I play. This is just the start of the program, that will set variables for calculation(which I haven't made yet:)). I was wondering why their are so many errors coming up. Thanks.
Here is the program:
// this is a program in progress
#include <stdio.h>
#include <iostream.h>
int main(int nNumberofArgs, char* pszArgs[])
{
//ask what thevery operation
int operationtype;
cout << "What thievery operation are you performing?\n";
cin >> operationtype;
cout << "\n(Rob Vaults=1, Rob Towers=2, Rob Granaries=3, Kidnap
Peasants=4, Steal Horses=5\n";
If (operationtype == 1)
{
// Action: Rob Vaults
//at war or not?
char war;
cout << "Are you at war with your target?(Y/N)";
cin >> war;
If (war == "Y")
{
//gains per thief at war
float gpt;
gpt = 80.0;
//resources lost at war
float rl;
rl = 0.1;
//set units to gc
string unit;
unit = "gc";
}
If (war == "N")
{
//gains per thief not at war
float gpt;
gpt = 52.0;
//resources lost not at war
float rl;
rl = 0.1;
//set units to gc
string unit;
unit = "gc";
}
}
return 0;
}
-
I don't think if has a capital "I". I dislike c++, so that is all the help I can offer.
and do
instead of
As the wise programmer once said: "Use single quotes for single characters."
declare all your variables at the beginning of the function
don't do
halfway through the function.
My corrected code HAS NOT BEEN TESTED
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int operationtype;
char war;
float gpt;
float rl;
char unit[20];
printf("What type of operation are you performing?\n");
printf("Rob Vaults=1 Rob Towers=2 Rob Granaries=3 Kidnap Peasants=4, Steal Horses=5\n>");
scanf("%d",&operationtype);
if (operationtype == 1)
{
printf("Are you at war with your target?\n>");
scanf("%c",&war);
if (war == 'y')
{
gpt = 80.0; // gains per thief at war
rl = 0.1; // resources lost
//set units to gc
strcpy(unit,"gc");
}
else if(war == 'N')
{
gpt = 52.0; //gains per thief not at war
rl = 0.1; //resources lost not at war
strcpy(unit,"gc");
}
}
return 0;
}
Could work, you never know.
-
-
revised 2
I hope you don't mind Brian, but I revised yours and translated it back into c++(what little you made C) and instead of using if statements for what action you want to do, unreged, I made them into switches, well, here's what I got, hope you don't mind about the c++ thing, i just don't know c. I compiled it with msvc 6 and it works pretty well. Also, instead of floats I used double. here ya go:
#include <string.h>
#include <iostream.h>
int main(int argc, char *argv[])
{
int operationtype;
char war;
double gpt, rl;
char unit[20];
cout << "What type of operation are you performing?\n";
cout << "Rob Vaults=1 \nRob Towers=2 \nRob Granaries=3 \nKidnap Peasants=4, \nSteal Horses=5\n>";
cin>>operationtype;
switch (operationtype)
{
case 1:
cout <<"Are you at war with your target?\n";
cin>>war;
if (war == 'y')
{
gpt = 80.0; // gains per thief at war
rl = 0.1; // resources lost
//set units to gc
strcpy(unit,"gc");
}
else if (war == 'n')
{
gpt = 52.0; //gains per thief not at war
rl = 0.1; //resources lost not at war
strcpy(unit,"gc");
}
break;
default:
cout<<"type in something valid";
}
return 0;
}
-
For declaring variables through the function, it is correct in c++. it is in c that you cant do it i think. I might be mistaken though.
-
>>> For declaring variables through the function, it is correct in c++.
It is legal in C++, that does not mean it is universally applauded as good practice. If you declare all your variables at the start then you, and anyone else who has to pick up your code later, knows where they are without searching.
-
Here is my new code. I got most of the bugs out but it still always comes out with a 0 or a string of weird characters.
#include <stdio.h>
#include <string.h>
#include <iostream.h>
int main(int argc, char *argv[])
{
int operationtype;
char war;
float gpt;
float rl;
string unit;
int acresyou;
int acrestarget;
int thieves;
float relativesize;
float finalanswer;
printf("What type of operation are you performing?\n");
printf("Rob Vaults=1 Rob Towers=2 Rob Granaries=3 Kidnap Peasants=4, Steal Horses=5\n>");
scanf("%d",&operationtype);
//operation 1
if (operationtype == 1)
{
printf("Are you at war with your target?\n(Y/N)\n>");
cin >> war;
if (war == 'N')
{
gpt = 80.0; // gains per thief at war
rl = 0.1; // resources lost
//set units to gc
unit = "gc";
}
else if(war == 'N')
{
gpt = 52.0; //gains per thief not at war
rl = 0.1; //resources lost not at war
strcpy(unit,"gc");
}
}
//operation 2
if (operationtype == 2)
{
printf("Are you at war with your target?\n(Y/N)\n>");
cin >> war;
if (war == 'y')
{
gpt = 21.0; // gains per thief at war
rl = 0.0; // resources lost
//set units to gc
strcpy(unit,"runes");
}
else if(war == 'N')
{
gpt = 21.0; //gains per thief not at war
rl = 0.0; //resources lost not at war
strcpy(unit,"runes");
}
}
//operation 3
if (operationtype == 3)
{
printf("Are you at war with your target?\n(Y/N)\n>");
cin >> war;
if (war == 'y')
{
gpt = 120.0; // gains per thief at war
rl = 0.1; // resources lost
//set units to gc
strcpy(unit,"grain");
}
else if(war == 'N')
{
gpt = 120.0; //gains per thief not at war
rl = 0.1; //resources lost not at war
strcpy(unit,"grain");
}
}
//operation 4
if (operationtype == 4)
{
printf("Are you at war with your target?\n(Y/N)\n>");
cin >> war;
if (war == 'y')
{
gpt = 0.333; // gains per thief at war
rl = 0.222; // resources lost
//set units to gc
strcpy(unit,"peasants");
}
else if(war == 'N')
{
gpt = 0.166; //gains per thief not at war
rl = 0.222; //resources lost not at war
strcpy(unit,"peasants");
}
}
//operation 5
if (operationtype == 5)
{
printf("Are you at war with your target?\n(Y/N)\n>");
cin >> war;
if (war == 'y')
{
gpt = 0.125; // gains per thief at war
rl = 0.0; // resources lost
//set units to gc
strcpy(unit,"war horses");
}
else if(war == 'N')
{
gpt = 0.125; //gains per thief not at war
rl = 0.5; //resources lost not at war
strcpy(unit,"war horses");
}
}
//ask other variables
cout << "How many acres do you have?\n>";
cin >> acresyou;
cout << "How many acres does your target have?\n>";
cin >> acrestarget;
cout << "How many thieves will you send?\n>";
cin >> thieves;
//calculate relative size
if (acrestarget / acresyou < acresyou / acrestarget)
{
relativesize = (acrestarget / acresyou);
}
else if(acresyou / acrestarget < acrestarget / acresyou)
{
relativesize = (acresyou / acrestarget);
}
//final calculate
finalanswer = thieves * relativesize * gpt * (1 - rl);
//print on screen
cout << "Your thieves will return with\n>";
cout << finalanswer;
cout << " ";
cout << unit;
return 0;
}
-
can you use the code tags for posting large portions of code, it makes it much easier to read :)
-
and you dont seem to be reading in the operation type anywhere, that might help.
Code:
cin >> operationType;
-
change the datattype of acresyou and acrestarget to float from int and then try..
-
since you seem to be using that "are you at war" thing so much, why dont you make a function that does it for you ? you could also use the switch for some things ..