Vending Machine Program Help!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Vending Machine Program Help!!

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Posts
    18

    Vending Machine Program Help!!

    Okay I need a little help with this problem
    Allow the customer to select a grid location (such as C5). Allow for both upper and lower case input (i.e. C5 and c5 are both valid). Continue to accept input until the user enters two zeroes ("00").
    While you may not assume valid user input, you may assume that input will be limited to two characters.

    If the input is reversed, such as 5C, output a message as shown in the example and process the input as if it was in the correct order (i.e., C5).

    If either the letter or the number of the input is out of range, output an error message as shown in the example.

    If the grid location is empty, output an error message as shown in the example.

    If the user enters two upper-case O's (OO) instead of two zeroes (00), terminate the program as if the input was two zeroes. Input of two lower-case o's (oo) is invalid.

    Now I have most of this done I have the fail condition a two dimensional array the problem that I am running into is I have my two variables set to "char" and in order to process the information through my arrays it has to be in numerical form. I have tried "atoi" but being as they are variables and not constants it won't let me convert them to a number

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    This can be looked at as a "state machine" problem. Certain things happen if a letter is the first character entered ... what are the alternatives? The letter might be too high/out of range - then you'll prompt the user to correct the input. If a number is input first again check range and respond but also correct. If the first input is valid you move on to the second input. Again, you'll test if it is ok - a number following a letter, a letter following a number - or two zeros. Check range. If the input is valid your program knows what it is supposed to do ...

    As to your last paragraph ... I don't know what you mean by "being as they are variables and not constants it won't let me convert them to a number"

    IF you are setting this up as an two dimensional array, why don't you set it up as a two dimensional array of chars? you won't need to do any kind of conversion - you'll just be checking the "ASCII" value of the chars to determine what is valid input ...

  3. #3
    Join Date
    Mar 2007
    Posts
    18
    We really haven't learned about the char arrays... he doesn't mind if we go beyond the bounds of what we have learned but I have no idea where a char array starts so when I set the perameters within the array I don't know if it starts at "array[A], array[0], array[!], etc." so that would be the main trouble with going with a char array for me.

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    An array is an array is an array. You deal with an array of chars in the same manner as you would use an array of ints or floats or arrays or strings or anything else.

    I would not implement a solution to this problem using arrays, but if you were to do so, my suggestion was that you store chars rather than ints [and you wouldn't have to do any conversion from alphanumeric to int, or vice versa].

  5. #5
    Join Date
    Mar 2007
    Posts
    18
    My instructor wants us to use a 2D array in this problem that is why I am doing it via array. It kind of confused me to because I had the solution before he threw that at us.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    So the "answer" or "action" is based on something stored at the intersection of the two indices?

    I guess I get it, now.

    You are evaluating the input, a char at a time, and determining if the individual char is a number or a letter, if it is within the proper range, and if the input is a letter, and then using the proper input to determine an index for your array.

    So why can't you use atoi to convert the char input to an int? atoi works on the value which is referred to by your variable, not the name of variable. If someone inputs "3" and store that input as your "firstInput" variable, and you determine that the ASCII value of the char indicates that it is a number, run atoi( firstInput ), and you will assign the result to an int variable which stores "3". If someone inputs c and you store it as "secondInput" and then test to determine if secondInput is lowercase or uppercase and then cast to an int and subtract the appropriate amount to determine it's "int value" [65 if upper and 97 if lower].
    Last edited by nspils; 03-10-2007 at 04:47 PM.

  7. #7
    Join Date
    Mar 2007
    Posts
    18
    I don't know why it won't work when I initially tried to us atoi(firstValue) it told me that it had an error something about a const char it really confused me. I found a site that said just to put in something to the effect of
    int newValue = firstValue - firstValue + 1
    the only problem with that is then for some reason my firstValue ends up switching to an int throughout the whole program. I don't know this one has me really confused

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Then you need to define your "char" values (firstValue and secondValue) as const char*'s [since this is the datatype which atoi takes as an argument]. You can structure your code along the lines of this, where isNumber and inRange are functions which determine whether or not the value is a letter or a number and if it is in the range of indices, and determineIntValue is a function which returns the "adjusted int value, with A and a = 0" of the given char:

    Code:
    const char* firstValue;
    cout << "Please input the first index: " << endl;
    cin >> firstValue;
    if( isNumber(firstValue) && inRange(firstValue) )
    {
        int firstIndex = atoi( firstValue );
    }
    else if( !isNumber(firstValue) && inRange(firstValue) )
    {
        int firstIndex =  determineIntValue( firstValue );
    }
    else
    {
        cout << "Try again." << endl;
    }
    Last edited by nspils; 03-10-2007 at 04:48 PM.

  9. #9
    Join Date
    Mar 2007
    Posts
    18
    what does the * after char do???

  10. #10
    Join Date
    Mar 2007
    Posts
    18
    I tried it and I still can't get it to work I don't know what the problem is here is the code that I have thus far:

    #include <iostream>
    using namespace std;
    const int MAX = 5;
    bool loop(char &firstNum, char &secondNum);
    void swap(char &firstNum, char &secondNum);
    bool check(char firstNum, char secondNum);
    void figure(int array[][MAX], const char firstNum, const char secondNum,
    int &firstDig, int &secondDig, int &cost);
    main()
    {

    int array[MAX][MAX] = {{35, -1, 90, 50, 75}, {55, 10, -1, -1, 20},
    {-1, 85, 40, 95, 60}, {15, -1, 65, 5, 100}, {70, 45, 25, 80, 30}};
    const* char firstNum;
    const* char secondNum;
    int firstDig;
    int secondDig;
    int cost;
    bool correct;
    cout << "Enter a grid location (such as B2, 00 to terminate)\n";
    while (loop(firstNum, secondNum))
    {
    swap(firstNum, secondNum);
    correct = check(firstNum, secondNum);
    if (correct == 1)
    {
    figure(array, firstNum, secondNum, firstDig, secondDig, cost);
    }
    }
    }

    bool loop(char &firstNum, char &secondNum)
    {
    cout << "Grid location: ";
    cin >> firstNum >> secondNum;
    if (firstNum == '0' && secondNum == '0' || firstNum == 'O' && secondNum == 'O')
    {
    return 0;
    }
    else
    {
    firstNum = toupper(firstNum);
    secondNum = toupper(secondNum);
    return 1;
    }
    }

    void swap(char &firstNum, char &secondNum)
    {
    if (firstNum < secondNum)
    {
    char thirdNum = firstNum;
    firstNum = secondNum;
    secondNum = thirdNum;
    }
    }

    bool check (char firstNum, char secondNum)
    {
    if (firstNum > 'E' || secondNum > '5')
    {
    cout << "Input error: letter range is A-E, number range is 1-5.\n";
    return 0;
    }
    else
    {
    return 1;
    }
    }

    void figure(int array[][MAX], const char firstNum, const char secondNum,
    int &firstDig, int &secondDig, int &cost)
    {
    firstDig = atoi(firstNum);
    }

  11. #11
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    the * indicates that the variable is a pointer to a char [char* means char pointer] - it holds the address of a char sitting somewhere in memory.

    You are using char in your procedure declarations, yet defining your firstNum and secondNum as char*. This won't compile.

    If you are not at a place where you know about pointers, it might be better for you to cobble together a procedure which does the same thing as atoi: pass in a char, evaluate it's ASCII value [ if testChar >=48 && testChar <=57 return (int)testChar - 48] and you get back an int.

  12. #12
    Join Date
    Mar 2007
    Posts
    18
    We haven't got to pointers yet... it is kind of odd that my instructor brought up the atoi command without teaching us about pointers. I finally got my program to work it passed the test bed and everything else for the class! Thank you for your help.

  13. #13
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Glad you worked it out!

Similar Threads

  1. Replies: 3
    Last Post: 03-06-2008, 07:57 AM
  2. Connecting to a Server Program HELP
    By sedricbenson@ho in forum C++
    Replies: 2
    Last Post: 11-07-2006, 07:58 AM
  3. Replies: 0
    Last Post: 12-15-2000, 10:07 PM
  4. Java Virtual Machine
    By Raj in forum Java
    Replies: 2
    Last Post: 04-14-2000, 07:39 PM
  5. NT priveleges from an ASP program
    By Matthew Solnit in forum authorevents.appleman
    Replies: 1
    Last Post: 04-11-2000, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center