Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,843 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,412 people online right now. Registration is fast and FREE... Join Now!




help

 
Reply to this topicStart new topic

help, help

dragonsword1
7 Feb, 2007 - 03:24 AM
Post #1

New D.I.C Head
*

Joined: 2 Feb, 2007
Posts: 21


My Contributions
hi can u help me i want to make a program that write from 4.0.0.0.000 to 5.0.0.0.000 in a txt file
i made it but it make only one number like"4.0.0.0.001"
and then overwrite on it the next number can u tell me how to make each number in its own line or write the code here is my code
CODE

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ofstream myfile ("example.txt");
    int num1=4, num2=0, num3=0, num4=0, num5=0;
    
    while(num1==4)
    {
        num2=0;
        while(num2<=9)
        {
            num3=0;
            while(num3<=9)
            {
                num4=0;
                while(num4<=9)
                {
                    num5=0;
                    while(num5<=999)
                    {
                        ofstream myfile;
                     myfile.open ("example.txt");
                     myfile <<num1<<"."<<num2<<"."<<num3<<"."<<num4<<"."<<num5<<endl;
                    myfile.close();
                                cout<<num1<<"."<<num2<<"."<<num3<<"."<<num4<<"."<<num5<<endl;
                           num5++;
                    }
                    num4++;
                }
                num3++;
            }
            num2++;
        }
        num1++;
    }
    cout<<"5.0.0.0.000";
    return 0;
}


This post has been edited by dragonsword1: 7 Feb, 2007 - 03:26 AM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Help
7 Feb, 2007 - 03:51 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,156



Thanked: 44 times
Dream Kudos: 125
My Contributions
Well you have problem of reopening and closing of file inside loop which overwrites previous data when you open file to write new. And it's inside loop.

So first thing you should do is take that file opening and closing out. I think that will solve the problem


CODE

    ofstream myfile ("example.txt");


remove this

and modify like this
CODE

ofstream myfile;
myfile.open ("example.txt");
while(num1==4)
    {
        num2=0;
        while(num2<=9)
        {
            num3=0;
            while(num3<=9)
            {
                num4=0;
                while(num4<=9)
                {
                    num5=0;
                    while(num5<=999)
                    {

                     myfile <<num1<<"."<<num2<<"."<<num3<<"."<<num4<<"."<<num5<<endl;
                                cout<<num1<<"."<<num2<<"."<<num3<<"."<<num4<<"."<<num5<<endl;
                           num5++;
                    }
                    num4++;
                }
                num3++;
            }
            num2++;
        }
        num1++;
    }
    myfile.close();
    cout<<"5.0.0.0.000";
    return 0;





make it something like this ok. I hope this will solve your problem.

This post has been edited by AmitTheInfinity: 7 Feb, 2007 - 03:55 AM
User is offlineProfile CardPM
+Quote Post

dragonsword1
RE: Help
7 Feb, 2007 - 04:00 AM
Post #3

New D.I.C Head
*

Joined: 2 Feb, 2007
Posts: 21


My Contributions
thnxxxxxxxxxx soooo much i done it

This post has been edited by dragonsword1: 7 Feb, 2007 - 04:08 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 10:04AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month