amiga.org
     
iconAll times are GMT -6. The time now is 03:59 PM. | Welcome to Forum, please register to access all of our features.

» Amiga.org » Operating System Specific Discussions » Amiga OS » Amiga OS -- Development » C/C++ programming

Amiga OS -- Development This particular forum deals with issues regarding development for all versions of AmigaOS.

Reply
 
Thread Tools Display Modes
Old 11-08-2005, 05:51 AM   #1
jonssonj
Technoid
Points: 7,944, Level: 59 Points: 7,944, Level: 59 Points: 7,944, Level: 59
Activity: 3% Activity: 3% Activity: 3%
 
Join Date: Oct 2003
Location: Sweden, Bjärred
Posts: 251
Default C/C++ programming

Hello!

I wonder how I can check that a input made by a user, really is an integer and not anything else.

F.eg.

#include<iostream>

using namespace std;

int main(void)
{
int test;
cout << "Enter integer: ";
cin >> test;

// Now I want to check that the user really entered an
// integer and not anything else.
}


BR
Jörgen Jönsson
jonssonj is offline   Reply With Quote
Old 11-08-2005, 06:27 AM   #2
countzero
Defender of the Faith
Points: 10,900, Level: 69 Points: 10,900, Level: 69 Points: 10,900, Level: 69
Activity: 4% Activity: 4% Activity: 4%
 
countzero's Avatar
 
Join Date: Mar 2005
Location: Tokyo
Posts: 1,937
Blog Entries: 1
Default Re: C/C++ programming

I'm not a C/C++ expert, but the simplest way to do it would be to take the input in a character string and parse each character if it's a value between 0-9. Then you can convert it into a integer variable.
countzero is offline   Reply With Quote
Old 11-08-2005, 07:00 AM   #3
Amiga1200PPC
Beginner
Points: 5,246, Level: 46 Points: 5,246, Level: 46 Points: 5,246, Level: 46
Activity: 0% Activity: 0% Activity: 0%
 
Amiga1200PPC's Avatar
 
Join Date: Sep 2002
Posts: 25
Send a message via ICQ to Amiga1200PPC
Default Re: C/C++ programming

Why do you want to check that anyways?
Amiga1200PPC is offline   Reply With Quote
Old 11-08-2005, 07:31 AM   #4
Cymric
Defender of the Faith
Points: 7,150, Level: 55 Points: 7,150, Level: 55 Points: 7,150, Level: 55
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2002
Posts: 1,031
Default Re: C/C++ programming

The easiest way is by making use of sscanf(). Assuming you have the user input in an array of char buf with its starting tabs, spaces and letter symbols removed, you can simply call on

retval=sscanf(buf, "%d", &intval);

If retval equals 1, you have bagged an integer, the value of which is in intval. The downside is that it breaks down when faced with input which exceed the capacity of int, and ignores any remaining text in the input line after a non-number symbol was parsed. Also, sscanf() will not tell you what the user input was when it returns 0 indicating no match was made. In this case, your best bet is to simply put out a 0x07-character (\b if memory serves me) for the terminal beep, warn the user with a text, and redo the input.
__________________
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
Cymric is offline   Reply With Quote
Old 11-08-2005, 08:03 AM   #5
mgerics
Technoid
Points: 9,481, Level: 65 Points: 9,481, Level: 65 Points: 9,481, Level: 65
Activity: 2% Activity: 2% Activity: 2%
 
Join Date: Jun 2002
Posts: 290
Default Re: C/C++ programming

Rusty C programmer here, but
try http://man.he.net/man3/atoi
or http://man.he.net/man3/strtol
mgerics is offline   Reply With Quote
Old 11-08-2005, 08:06 AM   #6
melott
Cult Member
Points: 9,136, Level: 64 Points: 9,136, Level: 64 Points: 9,136, Level: 64
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2002
Posts: 989
Default Re: C/C++ programming

You may want to consider joining this group ...
Amiga_BCG@yahoogroups.com

The guy running it is very good and responds
to all questions
__________________
Stealth ONE 8-)
melott is offline   Reply With Quote
Old 11-08-2005, 09:34 AM   #7
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: C/C++ programming

If you want properly validated input you generally need to do it yourself. There are lots of examples of char by char input through stdio.
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 11-08-2005, 09:45 AM   #8
uncharted
Defender of the Faith
Points: 10,899, Level: 69 Points: 10,899, Level: 69 Points: 10,899, Level: 69
Activity: 4% Activity: 4% Activity: 4%
 
Join Date: Mar 2002
Posts: 1,520
Default Re: C/C++ programming

Quote:
Karlos wrote:
If you want properly validated input you generally need to do it yourself. There are lots of examples of char by char input through stdio.
That's why Java is so much better :-)

/me runs away from Karlos
uncharted is offline   Reply With Quote
Old 11-08-2005, 09:49 AM   #9
Karlos
Sockologist
Points: 48,752, Level: 100 Points: 48,752, Level: 100 Points: 48,752, Level: 100
Activity: 8% Activity: 8% Activity: 8%
 
Karlos's Avatar
 
Join Date: Nov 2002
Location: I solve practical problems...
Posts: 16,606
Blog Entries: 18
Default Re: C/C++ programming

@uncharted

Or a library with validated input routines provided ;-)
__________________
OCA
This isn't SCSI... This is SATA!!!
I have CDO. It's like OCD except all the letters are in ascending order. The way they should be.
Core2 Quad Q9450 2.66GHz / X48T / 4GB DDR3 / nVidia GTX275 / Linux x64, AROS, Win64
A1XE 800MHz / 512MB / Radeon 9200 / OS4.1
A1200T BPPC 240MHz / 256MB / Permedia 2 / OS 3.1 - OS3.9, OS4
A1200T Apollo 1240 28MHz / 32MB / Mediator1200 / Voodoo 3000 / OS3.9
A1200D Apollo 1240 25MHz (ejector seat ROM edition) / 32MB
Karlos is offline   Reply With Quote
Old 11-08-2005, 09:58 AM   #10
smithy
Cult Member
Points: 7,413, Level: 57 Points: 7,413, Level: 57 Points: 7,413, Level: 57
Activity: 19% Activity: 19% Activity: 19%
 
Join Date: Mar 2002
Posts: 511
Default Re: C/C++ programming

Quote:
jonssonj wrote:
Hello!

I wonder how I can check that a input made by a user, really is an integer and not anything else.

int main(void)
{
int test;
cout << "Enter integer: ";
cin >> test;

// Now I want to check that the user really entered an
// integer and not anything else.
Add this:

Code:
   if(cin.fail())
   {
       // a number wasn't entered
   }
   else
   {
      if(!cin.eof())
      {
          // there is more non-numeric input
      }
   }
}
smithy is offline   Reply With Quote
Old 11-09-2005, 03:19 AM   #11
jonssonj
Technoid
Points: 7,944, Level: 59 Points: 7,944, Level: 59 Points: 7,944, Level: 59
Activity: 3% Activity: 3% Activity: 3%
 
Join Date: Oct 2003
Location: Sweden, Bjärred
Posts: 251
Default Re: C/C++ programming

Hello all!

I use two functions that solves the problem I had.

One from MSDN that clears the cin buffer and one function that I wrote myself, that checks if the input is an integer or not.

Code:
int main(void)
{
	char *buffer = 0;
	buffer = new char[11];
	int x=0;
	int nState=0;

	bool inmatning = true;

	while(inmatning)
	{
          // ................
          // other code goes here
          // ...................

          // I only saves 9 numbers so the biggest 
          // number a user can enter is 999999999.
          cin.getline(buffer,10);
          nState = ClearError(cin);
          if(CheckForInteger(buffer))
	  {
            x=atoi(buffer);
            if (x>=0)
	    {
              //.................
              // other code goes here..
              //..................
            } else
               inmatning=false;
          } else
              cout << "\nWrong input!";
        }
}


// Function from MSDN
int ClearError(istream& isIn)        // Clears istream object
{
    streambuf*  sbpThis;
    char        szTempBuf[20];
    int         nCount, nRet = isIn.rdstate();

    if  (nRet)                        // Any errors?
    {
        isIn.clear();                 // Clear error flags
        sbpThis = isIn.rdbuf();       // Get streambuf pointer
        nCount = sbpThis->in_avail(); // Number of characters in buffer

        while (nCount)                // Extract them to szTempBuf
        {
            if  (nCount > 20)
            {
                sbpThis->sgetn(szTempBuf, 20);
                nCount -= 20;
            }
            else
            {
                sbpThis->sgetn(szTempBuf, nCount);
                nCount = 0;
            }
        }
    }

    return  nRet;
}


// Function that checks if it's an integer or not.
bool CheckForInteger(char *input)
{
	// int max = 2147483647;


	// Initialize some variables.
	bool heltal = true;
	int length = 0;

	// continue until the last char.
	while(*input !='\0')
	{
		// if we find a '-' sign at the first pos.
		// we ignore it.
		if ( !(*input=='-' && length==0) )
		{
			// check if the current char is a 
                        // digit.
			if (!isdigit(*input))
			{
				// if not, we set the returnvalue to false.
				heltal = false;
			}
		}
		// go to next char and count allt the chars.
		input++; length++;
	}

	// go back to first char in buffer.
	input = input - length;

	// return result.
	return heltal;

}
Thanks for all your help and thanks for a great forum and a great community.

BR
Jörgen Jönsson

===========================================
Now I only owns one A500. I will buy a A-One when a new
model reaches the market (I'm optimistic )
jonssonj is offline   Reply With Quote
Reply

Bookmarks

Tags
programming , c or c , c++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Gal Pal programming trekiej CH / General 32 05-15-2008 12:22 PM
GAL programming help Iggy_Drougge Amiga Hardware Issues and discussion 2 10-09-2007 10:34 AM
RTG Programming Sidewinder Amiga OS -- Development 1 04-18-2004 03:41 AM
C Programming and MUI + MOS TheMagicM Amiga Software Issues and Discussion 1 08-11-2003 06:42 AM
MUD programming Desolator Amiga Software Issues and Discussion 6 02-12-2003 03:17 PM