Archived

This topic is now archived and is closed to further replies.

phil05

Console C++ Problem

Recommended Posts

phil05    100
I'm a beginner trying to make a form work on DOS.. aka you can see everything on the screen like you're making your in-game character stats. Can I do it somehow with this code? Right now it's waiting for me to hit enter, and it goes to the next line (the line shows up instead of showing already when executed). Any help is appreciated.
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <string>
using namespace std;		

void DrawColorString(string szText, int X, int Y, WORD color)
{	
	HANDLE outputH;
	COORD position = {X,Y};

	outputH = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(outputH, color);
	SetConsoleCursorPosition(outputH, position);

	cout << szText;
}

int main()
{
	std::string name;
	int age;

	// Red Bar
	DrawColorString("                                                                                ",  0, 0, BACKGROUND_RED);		
	// Name Input								
	DrawColorString("Your Name: ", 0, 2, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	std::getline(std::cin, name, '\n');

	DrawColorString("Your Age: ", 20, 4, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
	std::cin >> age;

	// End
	DrawColorString("", 0, 24, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    return 0;
}											
   
[edited by - philvaira on May 25, 2004 9:30:15 PM]

Share this post


Link to post
Share on other sites
_Twiggie_    122
Many people have had this problem before, i believe adding a std::cin.ignore( ) call will fix everything. The following code should demonstrate the problem you're having, with and without the method call to ignore( ).


#include <iostream>
#include <string>

int main(int argc, char *argv[])
{

char input = 0;
std::cout << "input first: ";
std::cin.get( input );
std::cout << "\n" << input << std::endl;
std::cin.ignore( ); // comment out this line and see the difference


std::cout << "input second: ";
std::cin.get( input );
std::cout << "\n" << input << std::endl;

return EXIT_SUCCESS;
}

edit: formated code

[edited by - _Twiggie_ on May 25, 2004 9:52:03 PM]

Share this post


Link to post
Share on other sites
phil05    100
Um.. I tried your code but I think you went off on some other problem. What I'm trying to do this this...

FULL SCREEN:
Name: _ // Both Name & Age should show up at the same time.
Age:

Instead of this...
SCREEN:
Name: _ (enter)
Age:_

EDIT: I think you were referring to this fix:
http://people.msoe.edu/~welch/courses/string.html

[edited by - philvaira on May 25, 2004 10:06:52 PM]

Share this post


Link to post
Share on other sites
izzo    437
You should draw all your strings on the screen before you start waiting for input from the user. Then, before every call to cin or getline, reposition the cursor to wherever you want the text that the user enters to appear. That should solve your problems.

cheers
sam

Share this post


Link to post
Share on other sites