2 more questions

1. How do I make the program go back to a particular line in a program.
As an example Basic has the goto command. I am trying to recreate a program I wrote in basic a long time ago, but C++ is not as easy to manipulate.

2. When should one use endl; as opposed to just using;. "end;" just seems to pop us randomly in the book in the same places where it seems I could put ";" and have the same result.






PS:I am using a book for beginners, and while it is keeping things simple there are many things that are not in detail, probably so people don't get confused. I will probably have to get a book that goes into a little more detail soon.
1. Use a loop such as do...while.
2. Depending on whether you need a newline character at the end of an output or not.
Search for online tutorials for details.
The best way to learn programming is through practice.
If I were you, for the 2nd question, I would write the following simple code and examine the difference on the console myself.
cout << "Hello!" << endl; cout << "Hello!";
Depending on your console environment and the program you wrote, that suggestion might backfire for the OP...

Question 1
If you are porting an old BASIC program, you have two basic options:

    1 direct port: use goto and other simple control constructs
    2 rewrite the program using more advanced constructs

I actually recommend the second method. Chances are that you will find and remove some bug, and you will learn more about C++.

Question 2
Output is a very specific issue. Here is wjee0910's information in a working example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <limits>
using namespace std;

int main()
  {
  cout << "This line uses 'endl'. (Press ENTER)" << endl;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  cout << "This line does not use 'endl'. (Press ENTER)";
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  cout << "All done!\n";
  return 0;
  }

The difference between endl and "\n" is that endl also flushes the stream (forcing all unwritten output to be written out to screen).

Hope this helps.
I am not porting the program. I am writing it from scratch using C++. The program is very simple, at least it was in basic anyway, but things in C++ are not so easy to do. I was basically able to return to line X from any point in the basic program by using the goto command.
I am thinking I will have to do a nested loop. They look so messy to me though. Thanks for the help everyone.

Topic archived. No new replies allowed.