GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Feb-2007, 17:39
masterspank74 masterspank74 is offline
New Member
 
Join Date: Feb 2007
Posts: 1
masterspank74 is on a distinguished road

Desperate Need for Assistance on C Program


I am a graduate level student and need to make change to my program, below is the code for my C Program, and the recommended changes, and I am stuck, wanted to see if I could get assistance from the great minds of C on the comment recommended changes below.
CPP / C++ / C Code:
#include <stdio.h>                         
#include <ctype.h>  
#include <stdlib.h>
#include <string.h>

#define FALSE (0)

int Validation_Check(char []);
        
main(void) // The following comment doesn't really provide a lot of info: // This is the start of the Currency Conversion Program II. 
{
    
    //The foreign conversion currency value rates are established here.
    float Australian_Dollar_Exchange_Rate = 0.79;
    float Brazilian_Real_Exchange_Rate = 0.48;  
    float Danish_Krone_Exchange_Rate = 0.18;
    float Swiss_Franc_Exchange_Rate = 0.81;
    float Swedish_Krona_Exchange_Rate = 0.14;
    float US_Dollars_Converted[5];
    float Foreign_Amt_For_Conversion;
    char Conversion_Amount_As_A_String[30];
    char Is_Entry_Valid;
    
    printf("Conversion Calculator:  "); //Prints the name of this program.
    printf("\nThis program will convert Brazilian Real into U.S. Dollars  ");  //Prints a description of the program.
    printf("\n");
    
         fflush(stdin); //Clear Invalid Data
         
         /*     
         Step 1. Get Amount - Great!
         */
         printf("\nEnter the amount of Brazilian Real to be converted:  "); //This prompts the end user to input the amount to be converted.
         gets(Conversion_Amount_As_A_String);
         
         /* 
         Step 2. Validate - Great!
         Note:
         In the following statement Is_Entry_Valid is initialized by the output
         of the function Validation_Check(). However, this function doesn't
         return 'Y' or 'N'. Check my comments in Validation_Check().
         */
          Is_Entry_Valid = Validation_Check(Conversion_Amount_As_A_String);
          
          /* 
          Step 3. Iterate as long as the amount entered is invalid! - Great!
          */
    
    while (Is_Entry_Valid == 'N'){
        /* 
        Getting here means that the amount entered is invalid!
        Therefore you should display an error message and start
        all over as you do below:
        */
        
         /*      
         Step 1. Get Amount - Great!
         */        
        fflush(stdin); //Clear Invalid Data
         printf("\nEnter the amount of Brazilian Real to be converted:  "); //This prompts the end user to input the amount to be converted.
         gets(Conversion_Amount_As_A_String);
         /* 
         Step 2. Validate - Great!
         Note:
         In the following statement Is_Entry_Valid is initialized by the output
         of the function Validation_Check(). However, this function doesn't
         return 'Y' or 'N'. Check my comments in Validation_Check().
         */         
          Is_Entry_Valid = Validation_Check(Conversion_Amount_As_A_String);
      }
      
      /* 
      Step 4. Convert string to float
      Note:
      Excellent! Getting here means that the amount entered represents
      a valid float quantity. So you convert the string to float.
      */
    Foreign_Amt_For_Conversion = atof(Conversion_Amount_As_A_String);

    /* 
    Step 5. Convert the foreign amount to US Dollars
    Note:
    (1) Excellent! Use the float quantity to perform the arithmetic and
        convert foreign currency to US Dollars.
        (2) Wouldn't be a good idea to display the exchange rate used in the
        conversion? What do you think?
    */                        
    US_Dollars_Converted[1] = Foreign_Amt_For_Conversion * Brazilian_Real_Exchange_Rate; //This establishes the conversion amount for the Brazilian Real to US Dollar.
    printf("%.2f Brazilian Real \t is $ %.6f U.S. Dollars\n", Foreign_Amt_For_Conversion, US_Dollars_Converted[1]);    
    
    /* 
    Note: Use group comments instead of end-of-line comments. Check also
    your Week 4 Individual Assignment Feedback for more information on
    Internal Documentation
    */
    fflush(stdin); //Clear Invalid Data
    printf("\n\nPress the [Enter] key to end the program");    //This instructs the end user how to end the program.                        
    getchar(); //This allows the screen to keep displaying until the Enter key is pushed. 
    
    return 0; //Main returning 0 to the OS
} 

/* 
Notes:

(1) You should document this function using a function prologue
(2) The function incorrectly rejects positive input such as +30.45
(3) Is a negative amount necessarily invalid? This function could be used
    by other programs for which negative amounts are allowed. Use a parameter
    to indicate whether or not negative amounts are accepted
*/
int Validation_Check(char User_Input[]) /*  Moved bracket to the next line. Functions are similar to main() */
{ 
      int Array_Element_ID, String_Length, nof_Decimal_Points = 0;
      char Is_It_Valid; // This is not initialized. How are you going to return 'Y'?
      String_Length = strlen(User_Input); 

      for(Array_Element_ID = 0; Array_Element_ID < String_Length; Array_Element_ID++) {
            if (isdigit(User_Input[Array_Element_ID]) == FALSE) {
                  if (User_Input[Array_Element_ID] == '-' ) {
                       /* 
                       There's no need to display an error message here and request new amount! 
                       Let the "caller" of Validation_Check() handle the user interface. 
                       Check my comments above.
                       */
                     printf("\n%s << Invalid Entry, Please Enter Valid Amount.\n", User_Input); //This prompts the end user to input a valid amount.
                     /* 
                     It's not really a good practice have multiple exit points from 
                     a function. Here you just need to set the return value; i.e.
                     Is_It_Valid = 'N'; and then terminate the loop either by using a
                     break statement or by setting Array_Element_ID = String_Length + 1
                       */
                           return Is_It_Valid = 'N'; //Passes data to a new transaction.
                      }
                else if (User_Input[Array_Element_ID] == '.' ) {
                    if (Array_Element_ID != 0 ) {
                                nof_Decimal_Points++;
                                if (nof_Decimal_Points >= 2) {
                           /* 
                           There's no need to display an error message here and request new amount! 
                           Let the "caller" of Validation_Check() handle the user interface. 
                           Check my comments above.
                           */
                                    printf("\n%s << Invalid Entry, Please Enter Valid Amount.\n", User_Input); //This prompts the end user to input a valid amount.
                         /* 
                         It's not really a good practice have multiple exit points from 
                         a function. Here you just need to set the return value; i.e.
                         Is_It_Valid = 'N'; and then terminate the loop either by using a
                         break statement or by setting Array_Element_ID = String_Length + 1
                           */
                                    return Is_It_Valid = 'N'; //Passes data to a new transaction.
                                }  
                          }  
                      }
                else {  
                      /* 
                      There's no need to display an error message here and request new amount! 
                      Let the "caller" of Validation_Check() handle the user interface. 
                      Check my comments above.
                      */
                           printf("\n%s << Invalid Entry, Please Enter Valid Amount.\n", User_Input); //This prompts the end user to input a valid amount.
                       /* 
                    It's not really a good practice have multiple exit points from 
                       a function. Here you just need to set the return value; i.e.
                       Is_It_Valid = 'N'; and then terminate the loop either by using a
                       break statement or by setting Array_Element_ID = String_Length + 1
                     */
                 return Is_It_Valid = 'N'; //Passes data to a new transaction.
                     } 
            }
      } 
      
      
      /* 
      Notes:
      (1) The following comment is wrong! Be careful when copying code!
      (2) Here instead of returning 0 (zero), you should make 
          the corrections above and return value; i.e. return Is_It_Valid;
          In other words this will be the single function exit point.
      */
      
      return 0; //Main returning 0 to the OS
}
Last edited by admin II : 02-Mar-2007 at 06:28. Reason: Please surround your C code with [cpp] ... [/cpp]
  #2  
Old 08-Mar-2007, 05:44
rumpl rumpl is offline
New Member
 
Join Date: Mar 2007
Posts: 3
rumpl is on a distinguished road

Re: Desperate Need for Assistance on C Program


CPP / C++ / C Code:
#include <stdio.h>                         
#include <ctype.h>  
#include <stdlib.h>
#include <string.h>

int Validation_Check(char [], int);

main(void)
{
  float Australian_Dollar_Exchange_Rate = 0.79;
  float Brazilian_Real_Exchange_Rate = 0.48;  
  float Danish_Krone_Exchange_Rate = 0.18;
  float Swiss_Franc_Exchange_Rate = 0.81;
  float Swedish_Krona_Exchange_Rate = 0.14;
  float US_Dollars_Converted[5];
  float Foreign_Amt_For_Conversion;
  char Conversion_Amount_As_A_String[30];
  char Is_Entry_Valid;
  
  printf("Conversion Calculator:\n");
  printf("This program will convert Brazilian Real into U.S. Dollars\n");
  
  fflush(stdin);
  
  printf("Enter the amount of Brazilian Real to be converted:  ");
  gets(Conversion_Amount_As_A_String);
  
  Is_Entry_Valid = Validation_Check(Conversion_Amount_As_A_String, 1);
  
  while (Is_Entry_Valid == 'N')
    {
      fflush(stdin);

      printf("Invalid Entry, Please Enter Valid Amount.\n");

      printf("Enter the amount of Brazilian Real to be converted:  ");

      gets(Conversion_Amount_As_A_String);

      Is_Entry_Valid = Validation_Check(Conversion_Amount_As_A_String, 1);
    }
  
  Foreign_Amt_For_Conversion = atof(Conversion_Amount_As_A_String);
  
  US_Dollars_Converted[1] = Foreign_Amt_For_Conversion * Brazilian_Real_Exchange_Rate;

  printf("%.2f Brazilian Real \t is $ %.6f U.S. Dollars\n", Foreign_Amt_For_Conversion, US_Dollars_Converted[1]);
  
  fflush(stdin);

  printf("Press the [Enter] key to end the program");

  getchar();
  
  return(0);
} 

/*
**  Function that checks the user_input, the function returns 'Y' if the string is a 
**  valid floating point number, 'N' otherwise. Empty string is considered non-valid. 
**  A valid number has only one '.' or ',', and the only valid non-digit character is '-', 
**  if it is in the begining of the number and if Negative_Numbers_Valid is 1.
**  Numbers begining with '+' are valid.
*/
int Validation_Check(char User_Input[], int Neg_Num_Valid)
{ 
  int Array_Element_ID;
  int String_Length;
  int nof_Decimal_Points = 0;
  char Is_It_Valid = 'Y';
  
  String_Length = strlen(User_Input); 
  
  if(String_Length == 0)
    Is_It_Valid = 'N';
  
  for(Array_Element_ID = 0; Array_Element_ID < String_Length && Is_It_Valid != 'N'; Array_Element_ID++) 
    {
      if (User_Input[Array_Element_ID] == '.' || User_Input[Array_Element_ID] == ',')
	{
	  nof_Decimal_Points++;

	  if (nof_Decimal_Points >= 2)
	    {
	      Is_It_Valid = 'N';
	    }
	}
      else if (!isdigit(User_Input[Array_Element_ID])) 
	{
	  Is_It_Valid = 'N';
	  
	  if (User_Input[Array_Element_ID] == '-' && Array_Element_ID == 0 && Neg_Num_Valid)
	    Is_It_Valid = 'Y';
	  
	  if (User_Input[Array_Element_ID] == '+' && Array_Element_ID == 0)
	    Is_It_Valid = 'Y';
	}
    }
  
  return(Is_It_Valid);
}
  #3  
Old 09-Mar-2007, 12:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Desperate Need for Assistance on C Program


Quote:
Originally Posted by masterspank74
I am a graduate level student and need to make change to my program, ...
Then you should already be aware of

this (main(void))
this (fflush(stdin);)
Quote:
Originally Posted by masterspank74
... below is the code for my C Program, and the recommended changes, and I am stuck, wanted to see if I could get assistance
Maybe it's just me, but trying to figure out what you need to change based on comments that AFAIK could simply be comments and not questions is more trouble than it's worth for a bunch of volunteers. Ask specific questions in your post and show us the code section you want to change and we can quickly give you the help you'd like.
__________________

Age is unimportant -- except in cheese
  #4  
Old 12-Mar-2007, 04:06
davis
 
Posts: n/a

Re: Desperate Need for Assistance on C Program


Quote:
Originally Posted by WaltP
www.gidnetwork.com (fflush(stdin);)

From the link:

Quote:
Originally Posted by WaltP
This means compilers do not have to define flushing of input streams. Now some have in fact defined this feature.

You may want to modify the content to remove the notion that this is a "compiler" issue, and refocus it on the C library provided by the toolchain or "compiler." Another choice would be to add/modify such:

"compiler vendors do not have to..."

...which may make the modification easier, but since it is fundamentally a C library implementation issue, I'd probably prefer to see that be the main focus in such a context.


:davis:
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 12:20
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
My first Java Program (Desperate for Help) Skyer Java Forum 9 14-Nov-2006 15:00
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 19:37
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:18.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.