Thread: Another question about syntax

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    15

    Another question about syntax

    Group,
    Again, self teaching has its limitations.
    Below is code that ultimately will answer question 3: design a program that finds all numbers from 1 to 1000 whose prime factors, when added together, sum up to a prime number.
    I am starting with a string limit of 1 to 100 to keep it manageable.
    I have labeled each unique output with a new name so ultimately I can harvest them. I think a few more iterations will gt me all that I need.
    The problem is,in the code below, the output starts at twenty. I do not understand why it does not output starting at least at 4 / 2 = 2.
    Can anyone help me?
    Thank in advance,
    Dan

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    
    int i;
    int j;
    int k;
    int l;
    int m;
    
    int main ()
    {
    
    
        // determine what the numbers prime factors are
    for ( int i = 2; i < 100; i++)
    {
    
        for ( int j = 2; j < i; j++ )
        {
            if ( i % j == 0 )
            {
            int k = i / j;
    
                cout << i << " i" << " div " << j << " j" << " = " << k << " k" << "\n";
    
                if ( k % j == 0)
                {
                    int l = j;
                    int m = k / l;
                    cout << "\t" << " and " << k << " k" << " div " << l << " lj" << " = " << m << " m" << "\n";
    
                }
    
            }
    
        }
    }
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The number of lines you output are likely exceeding the size of the console window buffer - which means the first bunch of lines are lost.

    When you have long outputs like this, consider writing the data to a file instead of (or in addition to) stdout, so you can examine everything that was printed.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    15
    Matticus,
    Thanks for the help. You are saying that I am getting the results that I want and thereby can use them in the next step, it is just that they are not being printed. If I reduce my scope to 50 I would see them.
    Writing the data (output) to a file is later in my learning path. Is it simple to describe?
    Thanks again,
    Dan

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You are saying that I am getting the results that I want and thereby can use them in the next step, it is just that they are not being printed.
    Well, they are getting printed, but the console window only has a certain amount of space before previous information is "scrolled off" the top.

    If I reduce my scope to 50 I would see them.
    Likely - have you tried?

    Writing the data (output) to a file is later in my learning path. Is it simple to describe?
    My knowledge lies with C, not C++, but there are plenty of tutorials online that explains file I/O. One of them is here.

    C++ File I/O Tutorial - Cprogramming.com

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just don't read from a file into a char array (use std::string). You might get a buffer overflow. I should notify webmaster of that old unsafe code o_O
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Syntax question
    By t3d in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2010, 08:07 PM
  2. Syntax Question
    By Beaner in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2006, 02:05 PM
  3. syntax question
    By InvariantLoop in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 07:58 AM
  4. question on syntax
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2004, 09:50 PM
  5. syntax question
    By Neoground1 in forum C++ Programming
    Replies: 1
    Last Post: 10-20-2002, 10:24 PM