Like Tree1Likes
  • 1 Post By Salem

My addition program doesn't work correctly.

This is a discussion on My addition program doesn't work correctly. within the C Programming forums, part of the General Programming Boards category; Code: int main() { int total_value=0; char mynum; do { printf("Line: "); scanf("%c",&mynum); mynum+=total_value; } while(mynum!='.'); printf("Total values are: %d,mynum); ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    140

    My addition program doesn't work correctly.

    Code:
     int main()
       {
       int total_value=0;
       char mynum;
       do
      {
       printf("Line: ");
       scanf("%c",&mynum);
        mynum+=total_value;
      } while(mynum!='.');
     
       printf("Total values are: %d,mynum);
    Basically i want the user to enter a word/sentence and from there i will store all the character they enter until they enter a period. If they enter ABC followed by a period. Then i add up all the values (ASCII) including the period.

    As of right now, my program only works if the user enters a period, as it'll jump straight to the printf function outside of the loop. And print out "46"

    Whats wrong?
    Last edited by tmac619619; 11-04-2012 at 02:02 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > mynum+=total_value;
    Does this update total_value?

    > printf("Total values are: %d,mynum);
    Does this print total_value?
    dennis.cpp likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I made a program but it doesn't work correctly
    By jeremy duncan in forum C Programming
    Replies: 30
    Last Post: 11-01-2011, 02:57 PM
  2. Uploading files doesn't work correctly
    By Devils Child in forum C# Programming
    Replies: 8
    Last Post: 05-21-2009, 07:51 AM
  3. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 04:40 AM
  4. linked list doesn't work correctly
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-12-2002, 11:32 AM

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