c++loopsfor-loopcoutlogic-error

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

here is my

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;
}

Solution

  • Here's your existing source 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;
    }
    

    -Tip- Don't use using std; at the global file scope level...


    #include <iostream>
    #include <conio.h>
    #include <vector> 
    #include <fstream>
    #include <string>
    
    void loadFile(const std::string filename, std::vector<unsigned int>& contents);
    unsigned int yourAlgorithm(unsigned int value);
    void unitTesting(std::vector<unsigned int>& fileContents, std::vector<unsigned int>& results);
    void printResults(std::vector<unsigned int>& values);
    
    int main() {
        std::vector<unsigned int> fileContents, results; 
        loadFile("samples.txt", fileContents);
        unitTesting(fileContents, results);
        printResults(results);
        getch();
        return 0;
    }
    
    // Functions down here:
    void loadFile(const std::string filename, std::vector<unsigned int>& contents) {
        // open your custom text file, either the values being separated by a space, comma delimiter, etc.
        // Get each value and store it into contents.
        std::fstream file(filename, std::ios_base::in);
        // you can do an addition check here to see if the file does exist and if it opened correctly... 
        unsigned int value;
        while (file >> value)
            contents.push_back(value);
        file.close();         
    }
    
    unsigned int yourAlgorithm(unsigned int value) {
        unsigned int sum = 0;
        for (int i = 1; i <= value; ++i) {
            sum += i;
    
            if (i == 33 || i % 10 == 3) {
                i = sum - i;
            }
        }
        return sum;
    }
    
    void unitTesting(std::vector<unsigned int>& fileContents, std::vector<unsigned int>& results) {
        unsigned int result = 0; 
        for (auto& v : fileContents) {
            result = yourAlgorithm(v);
            // I forgot to store the result in the result vector, now it's fixed.
            results.push_back(result);     
        }
    }
    
    void printResults(std::vector<unsigned int>& results) {
        for (auto& res : results) {
            std::cout << res << " ";
        }
        std::cout << '\n';
    }
    

    As you can see, I took your implementation and moved it into a function of its own. Then set up the code to read in a text file of integer values to populate a vector. This vector will be used to unit test your algorithm. I then moved your function or algorithm out of main altogether and called it within the unitTesting() function. I also separated the printing functionality and put that into its own function.

    Here, as you can see this code has good structure, is readable, modular and easy to debug. Something like this would be considered clean code, good practice. Now mind you, this is only Pseudo C++ code. I typed this as is and did not run this through any compiler or debugger. This was to demonstrate how you could unit test your code, but just running your algorithm in this manner still doesn't cut it, you still have to use your debugger and step through the code, line by line checking every calculation, every comparison, etc.!

    Being able to unit test your algorithms is one of the best ways to analyze any source code either being your own, or even someone else's code. I just demonstrated a powerful and useful tool that should be added to your toolset, it's called Unit Testing.

    Here's a good StackOverflow answer for reading in data from a file... Answer to Read Numeric Data from a Text File in C++


    Edit

    I had forgotten to add the line of code to store the results in the vector within the unitTesting() function. It is now updated.