News:

The forum has been updated to SMF (2.1.3)!
Please be patient as we work to polish up the place and update features as we can.

Main Menu

Your code, your questions

Started by Rolina, 18, September, 2010, 05:09:07 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Rolina

In this thread, we coders come together to help one another figure out what may be wrong with our code, and why it does not do what we are hoping it would.  This can range to errors you can't find to results you aren't expecting and more - if you're having problems, come here and seek help.




I'll start us out.

This is supposed to do several things, but I'm doing them one at a time.  I can make it enter values fine, but apparently it's not letting me enter and check for letters, meaning I'm probably doing it wrong.  Anyone care to correct my error?

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main ()
{
   srand (time(NULL));
   int base;    //spell's base chance of success
   float DmgMod;  //modifier for damage
   int DirMod;  //modifier for direction
   int TLuck;   //modifier for Target's luck stat.
   int result;  //checks to see if the spell lands.
   
   DmgMod = 0;  //Sets value to 0 for upcoming check
   result = rand() % 100 + 1;
   cout << "Testing for now" << endl << endl;
   
   cout << "Please insert base accuracy value: ";
   cin >> base;
   do {
       cout << endl << "Is the spell damaging? (Type Y/N): ";
       if (cin >> "N" | cin >> "n")
            DmgMod = 1;
       else if (cin >> "Y" | cin >> "y")
            DmgMod = 0.5;
       else
            cout << "Invalid entry.  Please enter Y or N";
       } while (DmgMod != 0);
   
   cout << base;
   fseek(stdin,SEEK_END,0);
   getchar();
}

Atrius (He/Him)

The problem is here:

if (cin >> "X" | cin >> "x")

Which returns an error like: ambiguous overload for 'operator>>' in 'std::cin >> "X"'

This is because ">>" is actually an overloaded operator.  Meaning it may look like an ordinary operator, but it's actually a function in disguise.  That function is designed to feed data from a stream into a variable.  By putting a constant there instead of a variable you've confused the compiler because it can't find a version of the function that operator represents that can handle storing data from the stream into a constant.
[sprite=220,4,0]I'm shaking my head in general disapproval of everything[/sprite]

Rolina

Yeah, I'm aware.  Should I make new variables just for the inputs?  Or would there be a more intelligent way of addressing the error?

Atrius (He/Him)

Since you want to compare it against both the upper & lower case value, it'll be more efficient to store it somewhere than to go through the trouble of retrieving it twice.
[sprite=220,4,0]I'm shaking my head in general disapproval of everything[/sprite]

Rolina

Okay!  I've done well so far, as far as I'm concerned.  However, I'm curious as to how to do something...

/*This program's goal is to produce a simple, yet user friendly module with
  which the testing team for the Empires of Weyard game: Golden Sun Empires can
  test the various aspects of the game.  This part of the tool's primary purpose
  is the testing of ailments within the game.                                 */

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main ()
{
    srand (time(NULL));
    int base;      //spell's base chance of success
    string DmgIpt; //string to determine damage mod.
    float DmgMod;  //modifier for damage
    int DirIpt;    //input to determine direction mod
    float DirMod;  //modifier for direction
    int TLuck;     //modifier for Target's luck stat.
    double check;  //the value the result must check
    int result;    //checks to see if the spell lands.
    string Cycle;  //Checks to test again
    int CycleVar;  //used to check for test cycle
   
   
    cout << "Testing for now" << endl << endl;

    loop:
   
    CycleVar = 2;  //Sets value to 2 for final check
    DmgMod = 0;    //Sets value to 0 for upcoming check
    DirMod = 2;    //Sets value to 2 for upcoming check
   
    //Base Accuracy Check.  All ailments have a base accuracy, inserted here.
   
    cout << endl << "Please insert base accuracy value: ";
    cin >> base;
   
    //Directional Check.  Accuracy of all attacks check for this.
   
    do {
        cout << endl << "What direction does the spell come from?" << endl
        << endl << "If from the front, press [1]"
        << endl << "If from the side, press [2]"
        << endl << "If from the rear, press [3]"
        << endl << "If it's a summon's effect, press [4]"
        << endl << endl << "Direction: ";
        cin >> DirIpt;
        if (DirIpt == 1)
             DirMod = 0.75;
        else if (DirIpt == 2)
             DirMod = 0.85;
        else if (DirIpt == 3)
             DirMod = 0.95;
        else if (DirIpt == 4)
             DirMod = 1.00;
        else
             cout << "Invalid entry.  Please enter 1, 2, 3, or 4." << endl;
        } while (DirMod > 1.5);
   
    //Damage check.  Note that Summons DO do damage, and will automatically
    //insert this setp.
   
    if (DirIpt == 4)
       {
           DmgMod = 0.5;
           goto Luck;
       }
   
    do {
        cout << endl << "Is the spell damaging? (Type Y/N): ";
        cin >> DmgIpt;
        if (DmgIpt == "N" | DmgIpt == "n")
             DmgMod = 1;
        else if (DmgIpt == "Y" | DmgIpt == "y")
             DmgMod = 0.5;
        else
             cout << "Invalid entry.  Please enter Y or N" << endl;
        } while (DmgMod < 0.5);
   
    //Luck check.  Target's luck has a large impact on the formula
   
    Luck:
    cout << endl << "Please insert the target's Luck stat: ";
    cin >> TLuck;
   
    //Final checks.  Numbers are plugged into the formula, and the RNG does its
    //thing.
   
    check = (base * DmgMod * DirMod) - (2 * TLuck);
    result = rand() % 100 + 1;
       
    if (check >= result)
         cout << endl << "TRUE:  Ailment succeeds" << endl;
    else cout << endl << "FALSE:  Ailment fails" << endl;
   
    cout << endl << "Value checked was: " << check;
    cout << endl << "1D100 RNG Results: " << result;
   
    //Checks to see if the user wants to try another test.
   
    do {
        cout << endl << endl << "Run test again? (Y/N): ";
        cin >> Cycle;
        if (Cycle == "N" | Cycle == "n")
             CycleVar = 0;
        else if (Cycle == "Y" | Cycle == "y")
             CycleVar = 1;
        else
             cout << "Invalid entry.  Please enter Y or N" << endl;
        } while (CycleVar > 1);
   
    if (CycleVar == 1)
        goto loop;
    else
        cout << endl << endl << "Test End";
    fseek(stdin,SEEK_END,0);
    getchar();
}


As you can see, I've got it working pretty well.  However, I'm curious... when you enter a non-numerical value on a numerical input, it... flips out into an endless loop.  I wanna try and write in way to prevent that, but... I've got no clue how.  Basically, an error that says:  "Error, value must be numerical in nature" or something instead of going into an endless loop.  Any ideas as to how I might do this?

Atrius (He/Him)

You'll probably end up having to create a function that stores it to a char array first, makes sure it's just numbers then converts it.
[sprite=220,4,0]I'm shaking my head in general disapproval of everything[/sprite]

Rolina

Heeeh...  No idea how to do that. @_@

Well, Internet Research, HO!