Thread: My addition program doesn't work correctly.

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

    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
    39,666
    > mynum+=total_value;
    Does this update total_value?

    > printf("Total values are: %d,mynum);
    Does this print total_value?
    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.

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, 01:57 PM
  2. Uploading files doesn't work correctly
    By Devils Child in forum C# Programming
    Replies: 8
    Last Post: 05-21-2009, 06: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