Menu and Console Input

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1118 Views - Last Post: 06 February 2011 - 07:01 PM Rate Topic: -----

#1 amaac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 11

Menu and Console Input

Posted 06 February 2011 - 01:13 PM

I have gone over my code countless times but i cannot seem to find the problem. My program crashes every time. I know i am passing variables right and declaring the right. I think it might have something to do with my For loops. Please help me as i have spent hours trying to change things around to get this to work. I had a much more advanced input parser but it was also crashing on me. so i made the input parser simple.


#include "myMenu.h"

myMenu::myMenu()
{
    ext = 0;
    string myIntChoices [3] = {"Check for Odd/Even","Check for Prime","Exit"};
    string myStringChoices [4] = {"Convert To UpperCase","Convert To LowerCase","Amount Of Characters", "Exit"};
    myIntptr = myIntChoices;
    myStringptr = myStringChoices;
    intArrSize = sizeof(myIntChoices);
    stringArrSize = sizeof(myStringChoices);
}
myMenu::~myMenu()
{}

void myMenu::printMenu()
{
    inputParser newParser;
    CinReader reader;
    while (ext < 1)
    {
        cout << "\nHello Welcome to my program. \nPlease enter i for numbers and s for strings followed by the data to work with.\n";
        cout << "Enter q to Quit\n";
        userInput = reader.readString(true);
        userInput = newParser.parseMe(userChoice, userInput);

        if (userChoice == 'S' || userChoice == 's')
        {
                //get the string without choice
                cout << userChoice;
                newMenu(myStringptr, stringArrSize);
                //startStr = userInput.find_first_of(" ");
                //tempString = userInput.substr(startStr, npos);
        }
        else if (userChoice == 'I' || userChoice == 'i')
        {
                //get the string without choice convert to an int
                cout << userChoice;
                newMenu(myIntptr, intArrSize);
        }
        else if (userChoice == 'q' || userChoice == 'Q')
            ext = 1;
        else
        {
            cout << "\nYou have entered your data wrong Please try again";
        }
    }
}

void myMenu::newMenu(string choices [], int numOfChoice)
{
    //cycle through the choices array and print out a menu
    for (int i = 0; i < (numOfChoice); i ++)
    {
        cout << "\n[" << (i+1) << "]" << choices[i] << endl;
    }
    cout << "Please enter your Choice";
}


#include "inputParser.h"

string inputParser::parseMe(char &userChoice, string userInput)
{
    userChoice = userInput[0];
    return userInput.substr(2, string::npos);
}


I jsut need to know if there are mistakes in my logic. or maybe i am not calling a function correctly

This post has been edited by amaac: 06 February 2011 - 01:27 PM
Reason for edit:: Title renamed to be more descriptive


Is This A Good Question/Topic? 0
  • +

Replies To: Menu and Console Input

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Menu and Console Input

Posted 06 February 2011 - 01:15 PM

what has the debugger told? do you know where it is crashing?
Was This Post Helpful? 0
  • +
  • -

#3 amaac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 11

Re: Menu and Console Input

Posted 06 February 2011 - 01:18 PM

View Postishkabible, on 06 February 2011 - 08:15 PM, said:

what has the debugger told? do you know where it is crashing?


actually i have only just begun to learn how to use the debugger. but i am not sure what everything means. or how exactly to debug it.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,322
  • Joined: 25-December 09

Re: Menu and Console Input

Posted 06 February 2011 - 01:20 PM

Quote

My program crashes every time.


How is it crashing? Are there any error messages? If so post them.

Otherwise post a complete program that illustrates the crash.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 amaac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 11

Re: Menu and Console Input

Posted 06 February 2011 - 01:21 PM

View Postishkabible, on 06 February 2011 - 08:15 PM, said:

what has the debugger told? do you know where it is crashing?


ok program recieved Segmentation fault
In msvcrt!flush() system32 file

it happened immediately after my input which was "s string"
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Menu and Console Input

Posted 06 February 2011 - 01:22 PM

This construct:

for (int i = 1; i < (numOfChoice+1); i ++)
{
    cout << "\n[" << i << "]" << choices[i-1] << endl;
}



is fraught with potential for misuse. Here's a clue: arrays in C and C++ -- indeed nearly every modern language -- are indexed starting at 0. DEAL WITH IT, don't TRY TO FIGHT IT! If you want to print 1 - 3 for the user's benefit, then do THIS:

for (int i = 0; i < numOfChoice; i ++)
{
    cout << "\n[" << i + 1 << "]" << choices[i] << endl;
}

Was This Post Helpful? 0
  • +
  • -

#7 amaac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 11

Re: Menu and Console Input

Posted 06 February 2011 - 01:25 PM

View PostJackOfAllTrades, on 06 February 2011 - 08:22 PM, said:

This construct:

for (int i = 1; i < (numOfChoice+1); i ++)
{
    cout << "\n[" << i << "]" << choices[i-1] << endl;
}



is fraught with potential for misuse. Here's a clue: arrays in C and C++ -- indeed nearly every modern language -- are indexed starting at 0. DEAL WITH IT, don't TRY TO FIGHT IT! If you want to print 1 - 3 for the user's benefit, then do THIS:

for (int i = 0; i < numOfChoice; i ++)
{
    cout << "\n[" << i + 1 << "]" << choices[i] << endl;
}


fair enough. but for the scope of this program the difference in output should be none.
and the program still crashes with the same error from debugger
but i did fix that..
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Menu and Console Input

Posted 06 February 2011 - 01:26 PM

Out of curiosity, what does reader::readString look like?

You really need to learn to use the debugger. What are you using as a development environment?
Was This Post Helpful? 0
  • +
  • -

#9 amaac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 11

Re: Menu and Console Input

Posted 06 February 2011 - 01:31 PM

View PostJackOfAllTrades, on 06 February 2011 - 08:26 PM, said:

Out of curiosity, what does reader::readString look like?

You really need to learn to use the debugger. What are you using as a development environment?


Ok Reader is from CinReader It is a class that my teacher created and has us use on all of our programs. i am using codeblocks. and i am trying to use the debugger but i do not know how exactly yet. or rather i do not know what i am looking for. the error i gave came from the debugger and the output that i saw has strings from CinReader but i cannot imagine that his code is wrong.. maybe i am using it wrong.

string readString (bool allowEmpty = true, unsigned int limitTo = 0);
		
		/**
		 * Enable or disable more verbose error messages for invalid integer inputs.
		 * Defaults to true.
		 * @param show 	if true, show additional ERROR output on invalid input; if false,
		 * 					display only intMsg as error prompt.
		 */

string CinReader::readString (bool allowEmpty, unsigned int limitTo)
{
	string input = "";
	getline(cin, input);
	if (!allowEmpty)
	{
		while (input.length() == 0)
		{
			cout << stringMsg;
			getline(cin, input);
		}
	}
	if (limitTo > 0)
	{
		if (input.length() > limitTo)
			input = input.substr(0, limitTo);
	}
	return input;
}

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Menu and Console Input

Posted 06 February 2011 - 01:37 PM

Here's the Wiki entry on debugging with Code::Blocks.

Here's a quick YouTube video.

You're looking for what the contents of the variables are at the time of the crash. One of them is somehow messed up/not what you expect. Then you use the evidence provided and logic -- because programming is all about logic, after all -- to deduce how they got to be messed up.

Here's my quick guess:

You are passing true to readString which allows an empty string to be returned. You try to parse the empty string and cause the crash. Your parser does not account for being passed an empty string.

This post has been edited by JackOfAllTrades: 06 February 2011 - 01:39 PM

Was This Post Helpful? 1
  • +
  • -

#11 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Menu and Console Input

Posted 06 February 2011 - 01:41 PM

#include "myMenu.h"

myMenu::myMenu()
{
    ext = 0;
    string myIntChoices [3] = {"Check for Odd/Even","Check for Prime","Exit"};
    string myStringChoices [4] = {"Convert To UpperCase","Convert To LowerCase","Amount Of Characters", "Exit"};
    myIntptr = myIntChoices;
    myStringptr = myStringChoices;
    intArrSize = sizeof(myIntChoices);
    stringArrSize = sizeof(myStringChoices);
}


I have a problem with this bit of your code.
Think about what those pointers are pointing to at different parts of your code.
Was This Post Helpful? 0
  • +
  • -

#12 amaac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 11

Re: Menu and Console Input

Posted 06 February 2011 - 02:11 PM

View PostJackOfAllTrades, on 06 February 2011 - 08:37 PM, said:

Here's the Wiki entry on debugging with Code::Blocks.

Here's a quick YouTube video.

You're looking for what the contents of the variables are at the time of the crash. One of them is somehow messed up/not what you expect. Then you use the evidence provided and logic -- because programming is all about logic, after all -- to deduce how they got to be messed up.

Here's my quick guess:

You are passing true to readString which allows an empty string to be returned. You try to parse the empty string and cause the crash. Your parser does not account for being passed an empty string.

Ok but even if i use false i am still passing it "s string" <-- not empty string. therefore that bool means nothing.

View Postsk1v3r, on 06 February 2011 - 08:41 PM, said:

#include "myMenu.h"

myMenu::myMenu()
{
    ext = 0;
    string myIntChoices [3] = {"Check for Odd/Even","Check for Prime","Exit"};
    string myStringChoices [4] = {"Convert To UpperCase","Convert To LowerCase","Amount Of Characters", "Exit"};
    myIntptr = myIntChoices;
    myStringptr = myStringChoices;
    intArrSize = sizeof(myIntChoices);
    stringArrSize = sizeof(myStringChoices);
}


I have a problem with this bit of your code.
Think about what those pointers are pointing to at different parts of your code.


i feel like you are trying to show me something that i am not seeing.. all through my code those pointers never change. they point to respective arrays that hold my menu strings.
because i pass them as pointers into
inputParser::newMenu

it is easier to pass pointers rather than a copy of an array right? and you can treat them the same
Was This Post Helpful? 0
  • +
  • -

#13 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Menu and Console Input

Posted 06 February 2011 - 03:06 PM

yes, but unfortunately you can not be sure that those pointers are pointing to what you believe.
given the rules of scope, variables are removed from your program after the part of the code that they are declared in
{
    int example1 = 0;
}
int example2 = example1;



this won't work because 'example1' does not exist anymore, and your compiler will tell you this.
What you are doing is like this

string * myStrPtr;
{
    string strArr[10];
    myStrPtr = strArr;
}
example_function(myStrPtr);




this probably wouldn't give you an error, however it is pointing to a variable that doesn't exist anymore.
So you could be changing anything when you use it.
Was This Post Helpful? 0
  • +
  • -

#14 amaac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 01-February 11

Re: Menu and Console Input

Posted 06 February 2011 - 03:21 PM

ok i understand what you are saying but i am not seeing the solution besides declaring the array in the function. but i thought there was a way to pass arrays to functions... by &reference maybe? but after the function is left behind i will be referencing something that doesnt exist anymore..

I changed my code to const char * but i could enot Edit the #1 post so here it is.
#pragma once
#include "CinReader.h"
#include "inputParser.h"
#include <string>
#include <iostream>

using namespace std;
class myMenu
{
    public:
    myMenu();
    ~myMenu();
    void printMenu();
    void newMenu(const char* choices [], int numOfChoice);
    private:
    const char * myIntChoices [3];
    const char * myStringChoices [4];
    string userInput;//,*myIntptr, *myStringptr;
    char userChoice;
    int ext, choice, intArrSize, stringArrSize;


};

#include "myMenu.h"

myMenu::myMenu()
{
    ext = 0;
    const char * myIntChoices [] = {"Check for Odd/Even","Check for Prime","Exit"};
    const char * myStringChoices [] = {"Convert To UpperCase","Convert To LowerCase","Amount Of Characters", "Exit"};
    const size_t intArrSize = sizeof(myIntChoices)/sizeof(myIntChoices[0]);
    const size_t stringArrSize = sizeof(myStringChoices)/sizeof(myStringChoices[0]);
    //myIntptr = myIntChoices;
    //myStringptr = myStringChoices;
    //intArrSize = sizeof(myIntChoices);
    //stringArrSize = sizeof(myStringChoices);
}
myMenu::~myMenu()
{}

void myMenu::printMenu()
{
    inputParser newParser;
    //CinReader reader;
    while (ext < 1)
    {
        cout << "\nHello Welcome to my program. \nPlease enter i for numbers and s for strings followed by the data to work with.\n";
        cout << "Enter q to Quit\n";
        cin >> userInput;
        //userInput = reader.readString(true);
        userInput = newParser.parseMe(userChoice, userInput);

        if (userChoice == 'S' || userChoice == 's')
        {
                //get the string without choice
                cout << userChoice;
                newMenu(myStringChoices, stringArrSize);
                //startStr = userInput.find_first_of(" ");
                //tempString = userInput.substr(startStr, npos);
        }
        else if (userChoice == 'I' || userChoice == 'i')
        {
                //get the string without choice convert to an int
                cout << userChoice;
                newMenu(myIntChoices, intArrSize);
        }
        else if (userChoice == 'q' || userChoice == 'Q')
            ext = 1;
        else
        {
            cout << "\nYou have entered your data wrong Please try again";
        }
    }
}

void myMenu::newMenu(const char* choices [], int numOfChoice)
{
    //CinReader reader;
    //cycle through the choices array and print out a menu
    for (int i = 0; i < (numOfChoice); i ++)
    {
        cout << "\n[" << (i+1) << "]" << choices[i] << endl;
    }
    cout << "Please enter your Choice";
    //return userinput choice
    //return reader.readInt(1, numOfChoice);
}


#pragma once
#include <string>

using namespace std;

class inputParser
{
    public:
    string parseMe(char &userChoice, string userInput);

    private:
};


#include "inputParser.h"

string inputParser::parseMe(char &userChoice, string userInput)
{
    userChoice = userInput[0];
    return userInput.substr(2, string::npos);
}




I have been working on this for a day now 2 different versions that compile but crash..
this is the full code and it does compile ... the input i have been using has been "s string" as per the instructions

This post has been edited by amaac: 06 February 2011 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Menu and Console Input

Posted 06 February 2011 - 04:08 PM

    const char * myIntChoices [] = {"Check for Odd/Even","Check for Prime","Exit"};
    const char * myStringChoices [] = {"Convert To UpperCase","Convert To LowerCase","Amount Of Characters", "Exit"};
    const size_t intArrSize = sizeof(myIntChoices)/sizeof(myIntChoices[0]);
    const size_t stringArrSize = sizeof(myStringChoices)/sizeof(myStringChoices[0]);



You understand that these definitions are local to their function? For example:

int a;
int main() { int a = 5;}



You understand that the variable named a in main has absolutely nothing to do with the global variable named a?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2