I've got a program to write and im struggling to start out..

read one integer, n, from a file, then loop the program n times, using indexes 1-n. I'm not really sure where to start with that statement... Im a bit rusty in c++ haha.

Each time through the loop the program has to tell if the number is divisible by 3. If divisible by 3, i have to use the setfill, and i know how to do that...i just need a little help in setting up the loop.

there is also else if the loop is divisible by 5, and else if the loop is divisible by 3 and 5, but if i can get a little help on one of them, I can do the rest myself.

Thank you very much for your help!!!!

post the code you know how to do then we'll talk about the rest.

I think you need to clarify what the problem is. What are you supposed to do n times to (array?) indexes 1-n? Read more data from file? Generate random numbers?

What does divisibility by 3 or by 5 have to do with using setfill?

Be sure you understand the problem, then write what you think will solve it. Post your code, explaining what you think it should do and any errors you find in what it actually does.

setting up the loop is easy

you read the number n then put a for loop

for(int i=0;i<n;i++)
{
// here your code for checking of divisibility of 'i' will appear. it'll be an if-then-else conditional check.
}

hope this helps

sorry for all the confusion guys...let me try to clear it up to the best of my ability.

ok so the purpose of this program is to read a number (n) from a file,
then the user tells what the number is. The program will loop (n) times, using indexes 1- (n). The program will find
if the number is divisible by 3, 5, or 3 and 5, and use a setfill
to distinguish it from the others. Once the program has finished,
it will print the sum of all the numbers.

for example I am trying to create code to make the program run and have this as the output:

EXAMPLE: If the user entered the number 16
=================================================
1 (just for your information, the numbers are all supposed to be in line
2 instead of having the numbers with no divisible number all the way to the left)
.........3
4
-------5
.........6
7
8
.........9
------10
11
........12
13
14
xxxx15
16

The sum of the numbers 1 - 16 is 136.

_____________________________________

And before you read the code, I have a few notes about the code in case you are wondering why I did what I did.

If the loop is divisible by 3, I have to call a function called three, and the same with the others. Its much easier to just write code to setfill it right under the loop, but he is making us use a function to call it.

Im gonna try and write some notes above the code to further explain what i did and what i am trying to do

``````#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void three();
void five();
void both();

int main()
{

//Output the header to look a little nice
cout<<"=============================="<<endl;

//Declaring the Variables
ifstream dataFile;
int n, sum;
int total=0;

//Open file and read n
dataFile.open("/export/home/vilma/public_html/!Data/x.x");
dataFile>>n;

//Asks the user to enter a positive number
cout<<"Enter a positive integer:";
cin>>n;
cout<<endl;

//For Loop
for(int i = 0; i<n; i++)
{

cout<<i<<endl;
sum += i;

if(n % 3 == 0)
three();

else if(n % 5 == 0)
five();

else if(n % 3 == 0;n % 5 == 0)
both();

//I honestly don't remember if otherwise is a form of a loop haha so im probably being stupid...if its not divisible by 3 or 5 or 3 and 5... we just have to set the proper setfill to match the other numbers
otherwise()
{
cout<<right<<setw(10)<<setfill(' ')<<n;
}

dataFile.close();
return 0;
}
}

cout<<"The sum of the numbers 1 - "<<n<<" is ";
cout<<sum;

void three()
{
cout<<right<<setw(10)<<setfill('.')<<n;
}

void five()
{
cout<<right<<setw(10)<<setfill('-')<<n;
}

void both()
{
cout<<right<<setw(10)<<setfill('x')<<n;
}``````

Thank you so much guys for all of your help and for your time!!! I look forward to hearing from you with a way better technique of doing this then i have come up with hahaha

``````int n, sum;
int total=0;``````

Make sure you initialize the variable sum to 0 here too because you are using it later here without initializing it:

``sum += i;``

You want to use && for "AND", not a semicolon below:

``````else if(n % 3 == 0;n % 5 == 0)
both();``````

Should be this:

``````else if(n % 3 == 0 && n % 5 == 0)
both();``````
``````//I honestly don't remember if otherwise is a form of a loop haha so im probably being stupid...if its not divisible by 3 or 5 or 3 and 5... we just have to set the proper setfill to match the other numbers
otherwise()
{
cout<<right<<setw(10)<<setfill(' ')<<n;
}``````

Close. It's actually "else" but you don't need the parentheses:

``````else
{
cout<<right<<setw(10)<<setfill(' ')<<n;
}``````

OK, your problem is more clear now.

Why do you read in n from the file, then get another value from the keyboard?

You need to write the functions to include a parameter where you pass n to them. The functions should be responsible for the output.

Your logic is generally OK, till the last bit:

``````else if(n % 3 == 0;n % 5 == 0)
both();

otherwise()
{
cout<<right<<setw(10)<<setfill(' ')<<n;
}``````

The condition of being both divisible by 3 and by 5 should be written as

``else if( n % 3 == 0 && n % 5 == 0 )``

There is no "otherwise", you just use a final "else". To keep your logic consistent, you should have a function that displays the nonmatching values.

that was my last post and I have edited the code some because and got no errors when compiling. The user isn't supposed to enter a number, the number(n) is read from the file. But when i run the program, nothing comes out except the ================== that i cout'ed... its supposed to read the n file and output what is in the file. I'm not sure why its not. Any suggestions?? thanks!!!

____________________________________________________________________
ok so the purpose of this program is to read a number (n) from a file. The program will loop (n) times, using indexes 1- (n). The program will find
if the number is divisible by 3, 5, or 3 and 5, and use a setfill
to distinguish it from the others. Once the program has finished,
it will print the sum of all the numbers.

for example I am trying to create code to make the program run and have this as the output:

EXAMPLE: n=16
=================================================
1 (just for your information, the numbers are all supposed to be in line
2 instead of having the numbers with no divisible number all the way to the left)
.........3
4
-------5
.........6
7
8
.........9
------10
11
........12
13
14
xxxx15
16

The sum of the numbers 1 - 16 is 136.

_____________________________________

If the loop is divisible by 3, I have to call a function called three, and the same with the others. Its much easier to just write code to setfill it right under the loop, but he is making us use a function to call it.

Im gonna try and write some notes above the code to further explain what i did and what i am trying to do

``````#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void three();
void five();
void both();

int main()
{

//Output the header to look a little nice
cout<<"=============================="<<endl;

//Declaring the Variables
ifstream dataFile;
int n, sum=0;
int loop=0;

//Open file and read n
dataFile.open("/export/home/vilma/public_html/!Data/x.x");
dataFile>>n;

//Loop
while(loop>=n)
{

cout<<n<<endl;
sum += loop;
loop++;

if(n % 3 == 0)
three();

else if(n % 5 == 0)
five();

else if(n % 3 == 0 && n % 5 == 0)
both();

//if its not divisible by 3 or 5 or 3 and 5... we just have to set the proper setfill to match the other numbers
else
{
cout<<right<<setw(10)<<setfill(' ');
}

dataFile.close();
return 0;
cout<<"The sum of the numbers 1 - "<<n<<" is "<<sum;

}
}

void three()
{
cout<<right<<setw(10)<<setfill('.');
}

void five()
{
cout<<right<<setw(10)<<setfill('-');
}

void both()
{
cout<<right<<setw(10)<<setfill('x');
}``````

Does the data file in fact exist? And in the location specified? Does it in fact contain an integer?

Test for successful file opening before ever trying to read from a file.

``````datafile.open ( "blah.txt" );
if( ! datafile )
{
//error handing here
}
//otherwise, it opened successfully, proceed.``````
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.