Hey all,

I'm new to C++, much more at home with mathlab or VBA so this is probably a simple problem for you guys. I am writing a program and I want to change values at the end of loop but keep answers from previous loop, i.e. in other programs this approach works:

int i = 1;
for (time = 0;time <= 518400; time = time + 0.02)
{
>> my calculations

load_effect(i) = (result from calculations);

i = i + 1;
}

I have been been flicking through books and online and I'm not sure if C++ has this option, so I was going to output the data in text files. But as I have 3-4 data types which I will need to reuse in the program I need to save them to separate text files. I am getting a:

c++ error C2086: 'myfile' : redefinition

message when I try and use a second output file. Can anyone please help with this problem. Thanks in advance.

Colm.

>I'm not sure if C++ has this option
What an option? The C++ has no any options. It's a very powerful algorithmic language so you can invent any algorithm or data structure and implement it in C++ language. For example, your program case is a simplest array using example. What's a problem?

>c++ error C2086: 'myfile' : redefinition
Obviously, you declare myfile stream variable twice (or more). Close stream then reopen it for another file - that's all.

Next time use code tag properly:
[code=c++] source

[/code]
However your snippet was written in a very strange language, it's not the C++ ;)...

Apropos: you will never get exactly 518400 in the loop example (for floating point time).

I think it would be easier to diagnose if we saw the actual code you were using. It's hard to tell what the problem is when the only part of the code given is a loop that doesn't contain what you're asking about.

It doesn't really matter, but you can shorten time = time + 0.02 into just time += 0.02 and simplify i = i + 1; into i++; . The += means the same as what you have, and the i++ just means increment i by 1.

@ ArkM: I think they were trying to give a brief overview of what the code looked like, not the actual code itself.

Thanks for the help lads, much appreciated.

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.