Thread: Im having a trouble at subtracting a number that contains 3 in a c++ loop

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    1

    Im having a trouble at subtracting a number that contains 3 in a c++ loop

    so i am having a trouble in subtracting a number that contains 3 in c++ i just couldnt get it right maybe you can help me in analyzing

    code:
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    {
    	int integer, sum=0;
    	cout<<"Enter an integer: ";
    	cin>>integer;
    	for (int i = 1; i <= integer; ++i) 
    	{	 
    		sum += i;
    		
    		if(i==33 || i%10 == 3)
    		{
    			i = sum - i;
    		}
    		
        }
    
    
        cout << "  The Sum is = " << sum;
    
    
        getch();
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i = sum - i;
    Well the questions you need to answer are
    - what are you subtracting
    - what are you subtracting it from

    > so i am having a trouble in subtracting a number that contains 3
    Sure, you have the "contains 3" bit sorted out, but then what?
    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. Having trouble with while loop
    By Mad_hatter69 in forum C Programming
    Replies: 2
    Last Post: 09-12-2017, 10:10 PM
  2. Question about subtracting char number
    By Valentas in forum C++ Programming
    Replies: 1
    Last Post: 02-19-2013, 01:24 PM
  3. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  4. Weird problem using decimals in a subtracting loop.
    By bndcntn in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2006, 12:21 AM
  5. do{}while loop trouble
    By Zalbik in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 11:25 AM

Tags for this Thread