Re: Structure Help

This is a discussion on Re: Structure Help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I got this program assignment in class and I'm not sure how to start it. It needs to ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Unhappy 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 ]--------------------------------------------------------------------

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    ah yes

    very nice function...:

    int isdigit(num);

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    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.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    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.");
    		}
    
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21