Need Help with C++

Started by
5 comments, last by Zahlman 17 years, 12 months ago
Hello, I was trying to practice using "for loops" because I just learned about them, and I tried creating a little counting program, but something has gone wrong, but I cannot see. My compiler(dev c++) says the error is in the "int menu" function (says too few parameters in 'int menu(int&)'. Please Help!
/**$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*$
$*%    PROGRAMMER NAME: BRANDON WALL   *$%
%*$    DATE:  4-22-06                  %*$
*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$*%$**/ 

//  Counting Numbers

#include <string>
#include <iostream>
#include <ctime>
#include <windows.h>
#include <cstdlib>

void clearScreen()
{
     system("cls");
}

void delay()
{
     Sleep(1000);
}

void waitScreen()
{
     system("pause");
}

int menu(int& output)
{
    std::cout << "::::::::::::::::\n"
              << "::            ::\n"
              << "::    Menu    ::\n"
              << "::            ::\n"
              << "::::::::::::::::\n\n"
              << "Your Choices -\n\n"
              << "1) Count to 20\n"
              << "2) Count to 20 Backwards\n"
              << "3) Count to 20 By Fives\n\n"
              << "Your Selection - ";
    std::cin >> output;
}

int selectionTest()
{
    int menuSelection;
    menu(menuSelection);
    
    switch (menuSelection)
    {
           case 1:
                    for(menuSelection = 0; menuSelection < 20; ++menuSelection)
                    {
                        clearScreen();
                        std::cout << "Counting To 20:\n"
                                  << menuSelection;
                                  delay();
                                  delay();
                                  break;
                    }
           case 2:
                    for(menuSelection = 20; menuSelection >= 20; --menuSelection)
                    {
                        clearScreen();
                        std::cout << "Counting from 20 Backwards:\n"
                                  << menuSelection;
                                  delay();
                                  delay();
                                  break;
                    }
           case 3:
                    for(menuSelection = 0; menuSelection < 20; menuSelection += 5)
                    {
                        clearScreen();
                        std::cout << "Counting To 20 By Fives:\n"
                                  << menuSelection;
                                  delay();
                                  delay();
                                  break;
                    }
           default:
                    std::cout << "\n\nSorry, That Option is Invalid.";
                              delay();
                              delay();
                              menu();
    }
}

in main()
{
    clearScreen();
    selectionTest();
    waitScreen();
    return 0;
}       

Advertisement
Hey bud,

Under the default case in the switch statement, you havn't passed in a variable by int& like you did in the one at the top of the same function.

                                  break;                    }           default:                    std::cout << "\n\nSorry, That Option is Invalid.";                              delay();                              delay();                              menu();  //<----- here    }


Hope that helps,

Dave
Well, first off, you have 'int' in front of the 'menu' function, but you dont return a value.

try this
void menu(int& menuselection)
int main()
on the bottom

or is that just a typo
also, in your 'for' loops, I would use a different varaible, instead of using menuSelection again, try using 'counter' or something as your variable

for (counter =0;counter<10;counter++)
you should instead of using a reference parameter the menu function should return a value:

int menu(int& output){    std::cout << "::::::::::::::::\n"              << "::            ::\n"              << "::    Menu    ::\n"              << "::            ::\n"              << "::::::::::::::::\n\n"              << "Your Choices -\n\n"              << "1) Count to 20\n"              << "2) Count to 20 Backwards\n"              << "3) Count to 20 By Fives\n\n"              << "Your Selection - ";    std::cin >> output;}


you should use

int menu(void)        //USE THIS INSTEAD!!!{    std::cout << "::::::::::::::::\n"              << "::            ::\n"              << "::    Menu    ::\n"              << "::            ::\n"              << "::::::::::::::::\n\n"              << "Your Choices -\n\n"              << "1) Count to 20\n"              << "2) Count to 20 Backwards\n"              << "3) Count to 20 By Fives\n\n"              << "Your Selection - ";    int output;    std::cin >> output;    return output;}


and when you call menu() you save the value in a variable

    int selectionTest(){    int menuSelection;    menuSelection = menu();    //see!!        switch (menuSelection)    ...
------------------------------"Carpe Diem!""Failure is the prequel of success"_.-:Jimbo:-._
0) Don't use your real name on the Internet except where community standards expect it (i.e. many Usenet groups) :)

1) Read the error messages that you get. They include a line number and a description of the problem. When it says "too few parameters", that's because - imagine this! - there aren't "enough" parameters written in the function call, i.e. not as many as are specified by the function declaration/definition.

2) Like the others said, use return values for functions that have a single obvious thing to return. Why would you choose an option that makes less intuitive sense *and* requires the caller to have a variable prepared ahead of time for the call (rather than simply to receive the result - although even that isn't needed here)? Also, any time you declare a return type other than void, you should make sure that something is returned from the function (unless an exception is thrown) in all cases.

3) Don't reuse your variables for different purposes.

4) You need to learn how to handle the situation where the input isn't even actually a number. This is not obvious so I will be spoon-feeding it below.

5) Don't repeat things that happen in several cases when you can put the code in one place instead. Also, think twice about writing a function that will hardly do anything at all. Functions exist not simply to give names to things, but to actually bundle those logical "processes" together. By the way, you didn't even have your break statements in the right place; they were inside the for-loops, so they would break from the for loop - unconditionally after one iteration, which is pretty much never what you want - and then fall through in the switch statement, which is also usually never what you want.

6) Don't add an artificial pause to the end of your program. Instead, learn to run your program from the command line. You can facilitate this with a batch file, if you must have your precious double-click goodness :)

#include <string>#include <iostream>#include <ctime>#include <windows.h>#include <cstdlib>#include <limits>const int delayTime = 2000; // 2 seconds.const std::string myPrompt =  "::::::::::::::::\n"  "::            ::\n"  "::    Menu    ::\n"  "::            ::\n"  "::::::::::::::::\n\n"  "Your Choices -\n\n"  "1) Count to 20\n"  "2) Count to 20 Backwards\n"  "3) Count to 20 By Fives\n\n"  "Your Selection - "// Note that the compiler interprets back-to-back string literals as if they// were a single joined-up literal; that lets you wrap them around lines.// I will redesign the menu() function so that it performs the loop of// presenting options, asking for input, and reporting an error if appropriate.// So that the function can be reusable, it accepts parameters for the// things that you might want to change: the prompt text, the valid range// of answers, and the error text.// Because we will receive input a line at a time, I will be skipping to end// of line on every read attempt, even if a valid answer is given. That avoids// any problems that may be caused by stray "garbage" on the user's previous// line.int menu(const std::string& prompt, int low, int high, const std::string& error) {  ASSERT(low <= high);  // We initialize the result to something that we know is out of range, so  // that the loop will not end until we read in something valid.  int result = low - 1;  while (true) {    std::cout << prompt;    std::cin >> result;    // Do the cleanup that I mentioned.    std::cin.clear();    std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');    if (result >= low && result <= high) { return result; } // got it!    std::cout << error; // and loop back to prompt again.  }}int main() {  system("cls");  std::string label;  int begin, end, increment;  switch (menu(myPrompt, 1, 3, "\n\nSorry, That Option is Invalid.")) {    // See? No variable to assign the result to.    case 1:    label = "Counting To 20:\n";    begin = 1; end = 21; increment = 1;    break;    case 2:    label = "Counting from 20 Backwards:\n";    begin = 20; end = 0; increment = -1;    break;    case 3:    label = "Counting To 20 By Fives:\n";    begin = 5; end = 25; increment = 5;    break;    default:    // We *can't* get here, unless there is a bug in menu().    ASSERT(false);  }  // Now do a loop based on the 'label' and 'increment' that were chosen.  for(int i = begin; i != end; i += increment) {    // Why "!="? Because we might be counting either up or down, and checking    // a <, >= etc. inequality wouldn't work for all cases.    // As an exercise, think carefully about why I set the begin and end values    // the ways that I did.    std::cout << label << i;    Sleep(delayTime);  }}


This topic is closed to new replies.

Advertisement