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 03-Nov-2009, 22:48
d0ll3rb1ll d0ll3rb1ll is offline
New Member
 
Join Date: Nov 2009
Posts: 6
d0ll3rb1ll is on a distinguished road

Troubles getting correct output


I want to add the numbers between x and y and not include x and y. So for example if x was 5 and y 9 the sum would be 21. When I run the program the output is 0. Hopefully someone can help me. I use the gcc compiler.

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

int main (void)
{
        double x, y, sum, z;

        printf("Enter the value of x: \n");
        printf("Enter a  value larger than x for  y: \n");
                scanf("&lf &lf", &x, &y);

        sum = 0;

        for((z = (x + 1)); (x < z && z < y); z++)
                {
                        sum = z + sum;

                }

        printf("%f\n", sum);


return 0;
}
Last edited by LuciWiz : 04-Nov-2009 at 02:16. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 04-Nov-2009, 00:12
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Troubles getting correct output


Please TAKE THE TIME AND EFFORT TO USE THE C++ TAGS TO ENCLOSE THE CODE YOU SEND! ... read the guidelines.

If you used full warnings when you compile you program you might see:
Code:
~> gcc -Wall -W -pedantic prog-1.c -o prog-1 prog-1.c: In function ‘int main()’: prog-1.c:9: warning: too many arguments for format
which is refering to:
CPP / C++ / C Code:
scanf("&lf &lf", &x, &y);
The problem there is that you used &lf instead of the correct format specifier symbol %lf .
I think you might rather do that section like this:
CPP / C++ / C Code:
  printf("Enter the value of x: \n");
  scanf("%lf", &x);
  printf("Enter a value larger than x for y: \n");
  scanf("%lf", &y);
Try that and see if it runs a little better.
 
 

Recent GIDBlogAccepted for Ph.D. program 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
run script command on ns2.26 newbie06 Computer Software Forum - Linux 65 19-Aug-2009 08:50
getting associative array to output correct including missing value bouton MySQL / PHP Forum 2 29-Sep-2006 03:42
Bit stuffing agnostos C Programming Language 17 12-Sep-2006 21:10
No output from loop function crystalattice C++ Forum 2 20-Dec-2004 21:39
urgent: problem + output filters jack Apache Web Server Forum 0 04-Feb-2004 23:32

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

All times are GMT -6. The time now is 08:25.


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