Hi! To Everyone! Could you tell me how I can use GCC with that file comunicatoin, because : g++ main.cpp program1.cpp -o program1.exe - not valid - It didn't saw program1.h and all declaration in him.

I had the task : Write a program that prints the numbers from 1 to 100. But for multiples of three print “Global” instead of the number and for the multiples of five print “Logic”. For numbers which are multiples of both three and five print “GlobalLogic"

//program1.h
#ifndef PROGRAM1_H
#define PROGRAM1_H 1
#include <iostream>
#define HUNDREDPLUS1 101
#define BEGIN1 1
#define K3 3
#define K5 5

    using std::cout;
    using std::endl;
    void Multiples_3_5();

#endif //PROGRAM1_H

//main.cpp
#include"program1.h"

int main (int argc, const char * argv )
{
  Multiples_3_5();

  return 0;
}// main

//program.cpp
/**
 * @file    program1.cpp
 * @brief   A source file for program1
 *
 *
 * Copyright 2014 by Nikolas9896,
 *
 * This software is the confidential and proprietary information
 * of Nikolas9896 ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Nikolas9896.
 **/


void Multiples_3_5( )
{
    unsigned short it = BEGIN1;
    while(  it < HUNDREDPLUS1 )
    {
        if( ( !( it % K3 ) && !( it % K5 ) )  )
        {
            cout << "GlobalLogic"<< it << endl;
        }
        else if( !( it % K3 ) )
        {
            cout << "Global" << it << endl;
        }

        else if( !( it % K5 ) )
        {
            cout << "Logic" << it << endl;
        }

        else
        {
            cout << it << endl;
        }

    ++it;
    }
}

Recommended Answers

All 5 Replies

If the compiler (actually pre-processor in this case) cannot find a file it is looking for, then you have not told it where to look, or put the file in the wrong place, or misnamed the file. What is the exact error message?

By the way, posting your code on Daniweb breaks this part of your licence:

You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with Nikolas9896.

as part of Daniweb's TOS:

Posts contributed to the community immediately become the property of DaniWeb upon submission.

Anywho, try compiling the "program1" unit and "main" unit seperately. Which unit cannot find the file?

If we had the error message like Moschops said we would know this, but sometimes it's better practice to break down the compilation so you can see which stage the error happens at and you don't need to recompile the project from the beginning. This can be automated with something like a makefile.

This is such a small problem that it seems more appropriate to just do it all in one short file:

Note: the print logic flow can be made clear here by using two bool 'flags' to track what has already been printed:

for( int i = 1; i < 101; ++i )
{
    bool global = false,
         logic = false;

    if( i % 3 == 0 ) { global = true; cout << "global"; }
    if( i % 5 == 0 ) { logic = true; cout << "logic"; }

    if( !(global || logic) ) cout << i;

    cout << endl;
}

I the absence of other information, I would recommend trying the -I option with the path to the header files in question.

g++ -Ipath\to\include main.cpp program1.cpp -o program1.exe

However, if they are in the path already that shouldn't be necessary, so there may be more to this than that.

Thanks for all whose helped
Finally version of program :) David W - thank for idea - I update algoritm of prog but I used only one mark...

//program1.h
#ifndef PROGRAM1_H
#define PROGRAM1_H 1
#include <iostream>
#define HUNDREDPLUS1 101 // High limit
#define BEGIN1 1 // Low limit
#define M3 3 //It's checked digits multiples of 3
#define M5 5 //It's checked digits multiples of 5

    using std::cout;
    using std::endl;
    void Multiples_3_5();

#endif//PROGRAM1_H

//program1.cpp
#include"program1.h" // THE ERROE WAS FOUND !)

void Multiples_3_5( )
{
    bool is_printed;
    for( unsigned short it = BEGIN1; it < HUNDREDPLUS1; ++it )
    {
        is_printed = false;

        if( !( it % M3 ) )
        {
            is_printed = true;
            cout << "Global";
        }
        if( !( it % M5 ) )
        {
            is_printed = true;
            cout << "Logic";
        }

        if( !( is_printed ) )
        {
            cout << it;
        }

        cout << endl;

    }
}

//main

#include"program1.h"

int main (int argc, const char * argv )
{
  Multiples_3_5();

  return 0;
}// main
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.