New Posts  All Forums:Forum Nav:

For Loop Not Working

post #1 of 8
Thread Starter 
The Program is supposed to take a number and add all the intergers leading up to that number.
Code:

#include <iostream>
#include <cmath>

using namespace std;


int main()
{
    
    double count=0, num;
    
    {
    cout<<"Please Enter A Positive Interger"<<endl;
    cin>>num;
}
  
  
   if (num<1)
   {
    cout<<"Enter A Positive Value That's greater than 1"<<endl;
    cin>>num;
    }
    
    
 for (int num; num<1; num--) 
      { 
        count++;
        num+=num;
      } 

    
       
cout<<"The Sum Of All The Intergers: "<<num<<endl;
    
    


system("pause");
return 0;    
}
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
post #2 of 8
Not sure what count is doing, since it is never used anywhere other than count++.

I think the for-loop should be

for (int num1 = num; num1>1; num1--){


So if I enter 2, num is 2. Then you enter the for-loop and num1 becomes 2. Then while num1 is greater than 1, you execute the for-loop and decrement num1 each time. The way you had it before, you only executed the for loop if num was less than 1, but you only let someone enter a number that was greater than one.


I am still a little confused on what exactly the for-loop is suppose to be doing.

If I enter five, then should the result be 15, which was the result of 5+4+3+2+1?
Edited by joemaniaci - 4/18/11 at 1:32pm
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
I Love Cheese
(13 items)
 
  
CPUMotherboardGraphicsRAM
2600k @ 3.4 Asus P8P67 Basic XFX 5970 2 gig Black Edition 2x 4 gig DDR 1600 G-Skill 
Hard DriveOSMonitorPower
2x500 gig WD Vista Ultimate 64-bit Dual 25" Hannspree LCD CM 750 Watt 
CaseMouse
Rosewill THOR Logitech Trackball 
  hide details  
Reply
post #3 of 8
You have num as the number input by the user. Therefore, in your for loop, you should be running from either 0 all the way up to the value num or you run from num all the way down to zero summing them up.

ex:

for (int i = 0; i <= num; i++)

or

for(int i = num; i >= 0; i--)
Secks Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 3.6ghz Gigabyte P35 DS3L BFG 8800gt OC 2gb Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
Samsung F3 1TB Samsung 20x dvd-rw Windows Vista Home Premium 32 Samsung 226bw 22" LCD 
PowerCase
Corsair VX550 Lian Li PC-7 Plus 
  hide details  
Reply
Secks Machine
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 3.6ghz Gigabyte P35 DS3L BFG 8800gt OC 2gb Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
Samsung F3 1TB Samsung 20x dvd-rw Windows Vista Home Premium 32 Samsung 226bw 22" LCD 
PowerCase
Corsair VX550 Lian Li PC-7 Plus 
  hide details  
Reply
post #4 of 8
Easy

while(count<1)
{
int count = Ask a number
}



for (int num=0; num
int total +=num;

Total = total;

this is c sharp but should be able to convert it to your C.
post #5 of 8
Code:
#include <iostream>
#include <cmath>

using namespace std;


int main()
{
    
    int count=0, num, i;
    

    cout<<"Please Enter A Positive Interger"<<endl;
    cin>>num;

  
  
     while (num<1)
     {
         cout<<"Enter A Positive Value That's greater than 1"<<endl;
         cin>>num;
     }
    
    
      for (i=num; i > 0; i--) 
      { 
             count+=i;
      } 

    
       
     cout<<"The Sum Of All The Intergers: "<<count<<endl;
    
    


     system("pause");
     return 0;    
}

Try that. I changed your if statement to a while loop so it will keep asking for another number as long as they keep entering values less than 1. Also I changed your for loop. It adds all numbers less than or equal to the given number. For example if given 4 it would add 4+3+2+1. If you didn't want the 4 added just replace i=num with i=num-1
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
Scream Machine
(9 items)
 
  
CPUMotherboardGraphicsRAM
i7-4770K Gigabyte Z87X-UD3H EVGA GTX 780 16GB DDR3 
Hard DriveCoolingOSMonitor
256GB Samsung 840 Pro Kraken X60 Windows 7 Shimian 2560x1440 
Case
Phantom 630 
  hide details  
Reply
post #6 of 8
Thread Starter 
Quote:
Originally Posted by joemaniaci;13177780 
Not sure what count is doing, since it is never used anywhere other than count++.

I think the for-loop should be

for (int num1 = num; num1>1; num1--){


So if I enter 2, num is 2. Then you enter the for-loop and num1 becomes 2. Then while num1 is greater than 1, you execute the for-loop and decrement num1 each time. The way you had it before, you only executed the for loop if num was less than 1, but you only let someone enter a number that was greater than one.


I am still a little confused on what exactly the for-loop is suppose to be doing.

If I enter five, then should the result be 15, which was the result of 5+4+3+2+1?

Yes that's what i's supposed to be doing. Thank you very much
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
post #7 of 8
Thread Starter 
You Guys are geniuses. Thank You
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i7 5820k @4.5GHZ Asus Rampage V Extreme  EVGA 980ti classified 64GB Corsair Vengeance 3200MHZ 
Hard DriveOptical DriveCoolingOS
[Samsung 950 Pro 512GB][2X Samsung 840 250GB Ra... 16X LITESCRIDE DVD-RW EK Double Res, Double Pump, GPU, CPU combo Windows 10 x64 
MonitorKeyboardPowerCase
55IN 4K  Ducky Shine Corsair RM 1000i Modular STH10 
Mouse
Corsair m95 
  hide details  
Reply
post #8 of 8
Hmm did you initilize num? i dont see a num= anything I m not sure though
RAWR
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 EVGA X58 SLI XFX 5850 Corsair XMS3 Tri Channel 6GB PC12800 
Hard DriveOSMonitorKeyboard
OCZ Vertex 2 60gb Windows 7 Professional Asus VW266H Logitech G11 
PowerCaseMouse
Corsair 850 TX Corsair 800D Logitech G5 
  hide details  
Reply
RAWR
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 EVGA X58 SLI XFX 5850 Corsair XMS3 Tri Channel 6GB PC12800 
Hard DriveOSMonitorKeyboard
OCZ Vertex 2 60gb Windows 7 Professional Asus VW266H Logitech G11 
PowerCaseMouse
Corsair 850 TX Corsair 800D Logitech G5 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming