LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2005, 03:24 PM   #1
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Rep: Reputation: 102Reputation: 102
getchar() problem. Read other threads.


I have read other threads and it helped me solve my problem sort of. Can someone please tell me why I had to add two getchar(); in my code in order for it to work. I took the Source code and compilied it in both windows and linux then ran it and got the same results.
Code:
/********************************
* FileName: cbcalc.c            *
*                               *
* Purpose: Caluclate checkbook  *
*                               *
* Author: Josh Stephens         *
*                               *
* Date Created: 10/18/2005      *
*                               *
* Revision: 1.0                 *
********************************/

#include <stdio.h>

int main()
{
   float starting_balance;  //Balance Currently in checkbook
   float trans_amount;      //Amount Transaction Was For
   float ending_balance;    //Amount after Transaction takes place
   char trans_type;         //(D) for Deposit and (W) for Withdraw

   // Module 1. Get information From Users

      printf("Please enter your starting balance:\t$");
      scanf("%f",&starting_balance);

      printf("Please enter (D) for Deposit and (W) for withdraw:\t");
      scanf("%s",&trans_type);

      printf("Please enter your Amount for Transaction:\t$");
      scanf("%f",&trans_amount);

  // Module 2. Take input and make decision then calculate based on transaction type

     if(trans_type == 'D')
	{
	  ending_balance = starting_balance + trans_amount;
	}
     else
	{
	  ending_balance = starting_balance - trans_amount;
	}

  // Module 3. Print information Back to user 

        printf("Your new balance is now: \t$%.2f",ending_balance);
        printf("\nPress any Key to quit!");
	getchar();
	getchar();
        return(0); 
}
Thanks for all the help. I am very new to programming in C.
 
Old 11-20-2005, 03:50 PM   #2
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
I've looked through your code and I fail to see where you're calling getchar()??

Maybe you could include more of your source file with a specific example of its usage?

Thanks.
Mistro116
 
Old 11-20-2005, 03:54 PM   #3
jstephens84
Senior Member
 
Registered: Sep 2004
Location: Nashville
Distribution: Manjaro, RHEL, CentOS
Posts: 2,098

Original Poster
Rep: Reputation: 102Reputation: 102
There was no real big reason. I justed wanted to let the user know that the program was finished.
 
Old 11-20-2005, 04:10 PM   #4
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Well your usage for this would be incorrect:

If I was writing this, I would do something like:

printf("\nPress any Key to Quit!\n\n");

if (getchar() != NULL)
{
exit (-1);
}

exit can be found in the stdlib.h library, so just #include <stdlib.h> in your programs.

Hope this helps.
Mistro116
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
The funniest threads you've read on LQ Mojojo General 6 09-14-2004 08:32 AM
Redhat font problem (I've read many threads) SwoRDF!sH Red Hat 0 08-29-2004 03:07 PM
Audigy Problems (And yes, I've read all the other threads) gsibble Linux - Hardware 2 08-12-2003 03:13 PM
rpm install, I've read all the threads!! pekuekfir Linux - Newbie 13 01-12-2002 04:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration