What did I do wrong?

This is a discussion on What did I do wrong? within the C++ Programming forums, part of the General Programming Boards category; I am new to programming with C++. I started to make a program that is eventually meant to be a ...

  1. #1
    Unregistered
    Guest

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    I don't think if has a capital "I". I dislike c++, so that is all the help I can offer.

    and do
    Code:
    if(war == 'y')
    instead of
    Code:
    if(war == "y")
    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
    Code:
    float blahblah;
    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.
    Last edited by Brian; 02-21-2002 at 09:49 PM.

  3. #3
    Unregistered
    Guest
    Thanks.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    33

    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;
    }
    Last edited by Dummies102; 02-22-2002 at 01:34 AM.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    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.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> 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.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Unregistered
    Guest
    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;
    }

  8. #8
    Unregistered
    Guest
    can you use the code tags for posting large portions of code, it makes it much easier to read

  9. #9
    Unregistered
    Guest
    and you dont seem to be reading in the operation type anywhere, that might help.

    Code:
    cin >> operationType;

  10. #10
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    change the datattype of acresyou and acrestarget to float from int and then try..

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    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 ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21