Begginer needing clarification on various input types.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Begginer needing clarification on various input types.

  1. #1
    Join Date
    Aug 2012
    Posts
    3

    Begginer needing clarification on various input types.

    Ok I tend to write walls of text to make sure all info is provided, I will try my best to keep this short. I am very confused on the proper usage of input. I've been doing this for about a week and have used cin >> simply because I can't seem to get strings to work, or getline() anyways. Ok so what im doing is getting input from user then using if statements to check if y or n was pressed and to return error if neither was pressed. I have things working fine but as we all know in console if I enter nn it will answer n to the next question as well I've read about arrays and C style strings but guess Im missing the implementation of it. How can I get ONE char from the user and if say nn is pushed it only takes the first n. This has been my problem for three days and finally had to ask someone for help. So if you could give me some pointers It would be greatly appreciated
    Also im happy to post my code if need be just didnt want to post it wrong considdering i dont know the BB code or whatever used to segregate my code from rest of post.... TY for listening and wait patiently for help

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,288

    Re: Begginer needing clarification on various input types.

    Quote Originally Posted by deltasrage View Post
    ... i dont know the BB code or whatever used to segregate my code from rest of post....
    Then why don't you want to read about it?
    There is an Announcement: Before you post.... section that you should have read before doing your first post!

    As for your main problem: did you consider creating GUI program rather than MS-DOS-like one?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2012
    Posts
    3

    Re: Begginer needing clarification on various input types.

    Here's the code I have written to test adding a key to inv when picked up and not added when n is pressed. I know its probably a huge mess lol, we will call it beginner soup :P anyways I appologise it's not even commented because its more a test to implement adding a key to a backpack every time one is picked up.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int nEnd()
    {
    	int nKill = getchar();
    	return 0;
    }
    
    int nGetKey()
    {
    	cout << "Will you pick up the key? " << endl;
    	char sKeyString;
    
    	keyloop:
    	
    	cout << " \nPress y or n " << endl;
    	cin >> sKeyString;
    
    
    	if (sKeyString=='y')
    	{
    		cout << "You picked up a key. " << endl;
    		int nHasKey;
    		nHasKey = 1;
    		return nHasKey;
    		cout << endl;
    	}
    
    	else if (sKeyString=='n')
    	{
    		cout << "You leave the key on the ground. " << endl;
    		int nHasNoKey;
    	    nHasNoKey = 0;
    		return nHasNoKey;
    		cout << endl;
    	}
    
    	else
    	{
    		   cout << "Invalid Entry " << endl;
    		   goto keyloop;
    	}
    	
    }
    
    int nAdd(int x, int y)
    { 
    	int nResult;
    	nResult=x+y;
    	return nResult;
    }
    
    int nSubtract(int x, int y)
    {
    	int nResult;
    	nResult=x-y;
    	return nResult;
    }
    
    	
    
    
    class Backpack
    {
    public:
    	int  m_nKey;
    	char m_nSword;
    	int  m_nPotion;
    };
    
    int main()
    {
    
    	Backpack nBpack;
    	nBpack.m_nKey = 0;
    	nBpack.m_nSword = 0;
    	nBpack.m_nPotion = 0;
    	
    	cout << "You have " << nBpack.m_nKey << " Keys. " << endl;
    	
    	int nHasKey;
    	nHasKey = nGetKey();
    	nBpack.m_nKey = nAdd(nBpack.m_nKey, nHasKey);
    	
    	
    	cout << "You have " << nBpack.m_nKey << " Keys. " << endl;
    	mainloop:
    	nHasKey = nGetKey();
    	nBpack.m_nKey = nAdd(nBpack.m_nKey, nHasKey);
    
    	cout << "You have " << nBpack.m_nKey << " Keys. " << endl;
    	goto mainloop;
    
    	int nClose;
    	cin >> nClose;
    	return 0;
    }
    As far as a GUI based app just haven't played with that yet, only been at it a week and so console has been enough to at least return results so i know my code is running properly.

  4. #4
    Join Date
    Aug 2012
    Posts
    3

    Re: Begginer needing clarification on various input types.

    Here's the code I have written to test adding a key to inv when picked up and not added when n is pressed. I know its probably a huge mess lol, we will call it beginner soup :P anyways I appologise it's not even commented because its more a test to implement adding a key to a backpack every time one is picked up.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    
    int nEnd()
    {
    	int nKill = getchar();
    	return 0;
    }
    
    int nGetKey()
    {
    	cout << "Will you pick up the key? " << endl;
    	char sKeyString;
    
    	keyloop:
    	
    	cout << " \nPress y or n " << endl;
    	cin >> sKeyString;
    
    
    	if (sKeyString=='y')
    	{
    		cout << "You picked up a key. " << endl;
    		int nHasKey;
    		nHasKey = 1;
    		return nHasKey;
    		cout << endl;
    	}
    
    	else if (sKeyString=='n')
    	{
    		cout << "You leave the key on the ground. " << endl;
    		int nHasNoKey;
    	    nHasNoKey = 0;
    		return nHasNoKey;
    		cout << endl;
    	}
    
    	else
    	{
    		   cout << "Invalid Entry " << endl;
    		   goto keyloop;
    	}
    	
    }
    
    int nAdd(int x, int y)
    { 
    	int nResult;
    	nResult=x+y;
    	return nResult;
    }
    
    int nSubtract(int x, int y)
    {
    	int nResult;
    	nResult=x-y;
    	return nResult;
    }
    
    	
    
    
    class Backpack
    {
    public:
    	int  m_nKey;
    	char m_nSword;
    	int  m_nPotion;
    };
    
    int main()
    {
    
    	Backpack nBpack;
    	nBpack.m_nKey = 0;
    	nBpack.m_nSword = 0;
    	nBpack.m_nPotion = 0;
    	
    	cout << "You have " << nBpack.m_nKey << " Keys. " << endl;
    	
    	int nHasKey;
    	nHasKey = nGetKey();
    	nBpack.m_nKey = nAdd(nBpack.m_nKey, nHasKey);
    	
    	
    	cout << "You have " << nBpack.m_nKey << " Keys. " << endl;
    	mainloop:
    	nHasKey = nGetKey();
    	nBpack.m_nKey = nAdd(nBpack.m_nKey, nHasKey);
    
    	cout << "You have " << nBpack.m_nKey << " Keys. " << endl;
    	goto mainloop;
    
    	int nClose;
    	cin >> nClose;
    	return 0;
    }
    As far as a GUI based app just haven't played with that yet, only been at it a week and so console has been enough to at least return results so i know my code is running properly.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Begginer needing clarification on various input types.

    Quote Originally Posted by deltasrage View Post
    How can I get ONE char from the user and if say nn is pushed it only takes the first n. This has been my problem for three days and finally had to ask someone for help. So if you could give me some pointers It would be greatly appreciated
    Have a look at: http://www.parashift.com/c++-faq-lite/input-output.html
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center