The question i have is - how do i go to a specific (text line for example) in my code and replace it with something else (also a text line?) if a specified condition is fulfilled?
For example:
1 2 3 4 5 6 7 8 9
cout << "Player 1" << endl;
cout << "Enter "0": " << endl;
cin >> x;
if (x == 0)
{
// Goes to the first line, where "Player 1" is and replaces it with:
cout << "Player 2" << endl;
}
The output should be something like that:
Player 1
Enter "0":
>>
Player 1
Enter "0": 0
>>
Player 2
Enter "0": 0
I've searched through the forums, google, wiki and stuff, found nothing that would fulfill my request, like /r or goto with huge additional programs to it. Is there a simple way to do it or do i really have to use the goto?
Your question seems to be confused. I don't think you want to change your code as the program runs. I think you want to change what is displayed on screen as your code progresses? Is that right. So that, to use your example above, you want to show this on screen:
Player 1
Enter "0":
and then if the player enters 0, you want that display on screen to change to this:
Player 2
Enter "0": 0
That's not changing your code - at the point of running your program, the text you typed in as code essentially does not exist. It has been compiled and linked.
I think you want to control what's on screen, not change your code mid-execution. Is that right? If so, you should be doing this with functions. Not a clumsy goto call.
Yes, i want the program to run >> print "Player 1" for example >> user fulfills some condition >> "Player 1" is replaced by "Player 2".
I dont want it to be like:
Player 1
stuff
Player 2
stuff
player 1
stuff
player 2
stuff
I want all this Player 1 - Player 2 stuff to be on the top line, changing accordingly with the users actions.
// As for goto - i read about it, about why is it bad and why i should not use it, so i really want to find another solution.
// I though about using a function... and then... how would i do it? There should be some statement which would go to the place i would specify and do the deal... but then again, what would this statement be?
Firstly, be aware that the standard console is not conducive to this sort of thing. Look into ncurses or similar to get the fine control you're asking for on the console output.
Anyway, that said, something like this might start you off. It uses a simple loop. Where to take it depends on what you intend to do next.
#include <iostream>
usingnamespace std;
int main()
{
int userInput = 0;
for (int i=0; i<4; ++i)
{
cout << "Player " << i << endl;
cout << "Enter: ";
cin >> userInput;
if (userInput ==0)
{
continue;
}
else
{
cout << "User did not enter zero... do other stuff here?";
break;
}
}
return 0;
}
The output of this simply goes on the next line, as you'll see. ncurses or another such console library will make it easier for you to replace existing output on screen, rather than simply using the next line.
So, you're saying there is no way to reverse the \n with some stuff that will go to a previous line other than actually using the whole goto or the ncurses thing?
It seems strange to me that you can go to a new line but can't go back :)
Oh well, thanks for the help, i will try looking into ncurses.
What do you think goto does? It won't magically make your code run backwards in time, erasing things from the screen. It's just a brutal way of jumping around in the code. Anything already on the screen won't be deleted just because you jumped back to before that piece of code.
The basic console was never intended for this. If you want to do things that it was never meant for, you either use something else (for example, ncurses or depending on your OS, some extra headers and libraries that extend its capabilities), or resort to ugly kludges. You might as well complain that the basic console is rubbish for displaying pictures - it's just not what it's for.
// Names.cpp : Defines the entry point for the console application.
//
#include <stdafx.h> // used in MS VC++ - remove this line if using different compiler
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <windows.h>
usingnamespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int,int);
void gotoXY(int,int,string);
int main()
{
string name;
gotoXY(6,3,"Please enter three names.");
for(int i=0;i<3;i++)
{
gotoXY(5,6);
cout << "Enter name number " << i+1<< ", please.";
gotoXY(5+(i*10),8); // gotoXY(3,4) would print in column 3, line 4
cin>>name;
}
system("PAUSE");
return 0;
}
void gotoXY(int x, int y)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}