-
Re: Structure Help
Hey guys, I got this program assignment in class and I'm not sure how to start it. It needs to prompt the user to input a number and then checks each digit of the number to see whether it is a valid integer (ie. Characters are invalid). If it's not a number, it asks the user to enter another number, if it is a valid number, it outputs the number to the screen.
Now if anyone could help with with just the different functions and such I might use, that would be of great help. Below is what I've got so far.
//-----------------------------------------------------//
// NAME: Christopher Jackson //
// CLASS: CSC142-02 //
// SEMESTER: Spring 2002 //
// ASSIGNMENT: Program Assignment No. 1 //
//-----------------------------------------------------//
//----------------------------------------------------------------------------------------
// Program 1
// DESCRIPTION:
//----------------------------------------------------------------------------------------
#include <iostream>
bool digitCheck(string);
bool invalidChar();
void validOutput();
void invalidOutput();
//-----[ fUNCTION mAIN ]------------------------------------------------------------------
int main()
{
bool validString;
string number;
cout << "Please enter an integer: ";
cin >> number;
validString = digitCheck(number, validString);
output;
return 0;
}
//-----[ fUNCTION dEFINITIONS ]-----------------------------------------------------------
bool digitCheck(string num, bool vS)
{
for (count = 0; count <= 10; count++)
{
char currentChar = num[count];
if (count==0)
{
switch (currentChar)
{
case - :
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 : break;
default: invalidChar(vS); break;
}
}
else
{
switch (currentChar)
{
case - : invalidChar(vS); break;
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 : break;
default: invalidChar(vS); break;
}
}
}
vS = true;
return vS;
}
bool invalidChar(bool vS_)
{
vS_ = false;
return vS_;
}
void validOutput()
{
}
void invalidOutput()
{
}
//-----[ eND pROGRAM ]--------------------------------------------------------------------
-
ah yes
very nice function...:
int isdigit(num);
-
or if you want to write your own
Code:
bool IsDigit(char c)
{
return(c >= 0x30 && c <= 0x39);
}
if(IsDigit(num[i]) || (i == 0 && num[0] == '-'))
{
// valid number
}
hope this helps
U.
-
Code:
using System;
class Cpp
{
public static void Main()
{
int number;
Console.Write("Enter a number: ");
try
{
number = Convert.ToInt32(Console.ReadLine());
Console.Write("You entered: " + number);
}
catch
{
Console.WriteLine("Bad data exception.");
}
}
}