Errors When I Color

This is a discussion on Errors When I Color within the C++ Programming forums, part of the General Programming Boards category; In this program I am trying to color the answers. If the answer is correct it is green and if ...

  1. #1
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65

    Question Errors When I Color

    In this program I am trying to color the answers. If the answer is correct it is green and if incorrect it is colored red. But it wont color the individual letters. Il'l mark the area with the important stuff.

    Yes the code is long but i marked the part that needs to be looked at.

    FYI: There are no actual errors, it runs fine.

    If you hadnt noticed, the source file is under this post, so you can run it yourself. It will make more sence if you do.
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include "apmatrix.h"
    #include "apvector.h"
    #include <stdlib.h.>
    #include <windows.h>
    
    //Easy Coloring Of Words/Letters
    #define Color_White SetConsoleTextAttribute(stdOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
    #define Color_Red	SetConsoleTextAttribute(stdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    #define Color_Green SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    
    HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    int main()
    {
    	Color_White
    	//Welcome
    	cout<<"***Welcome to Quiz Machine***"<<endl<<"*****************************"
    		<<endl<<"Please follow the directions"<<endl<<"to see the"
    		<<" results of your"<<endl<<"True/False test."<<endl<<endl;
    	
    	//Declaration of Varibles
    	apvector<int> student(2);
    	apmatrix<char> score(10, 3, 'F');
    	apvector<int> count(2);
    	apvector<char> grade(2);
    	int size, size2;
    	
    	//Gets amount of students
    	cout<<"How many students took the test? ";
    	cin>>size;
    	size2 = size +1;
    
    	//Resises vectors and matrix
    	student.resize(size);
    	score.resize(10, size2);
    	count.resize(size);
    	grade.resize(size);
    
    	for(int t=0; t<size; ++t)
    		count[t] = 0;
    	for(t=0; t<size; ++t)
    		grade[t] = '-';
    
    	//Insert answer key
    	cout<<"Enter the correct answers(10):"<<endl;
    		for(int x = 0; x<10; ++x)
    		{
    			cin>>score[x][0];
    			if(score[x][0] == 't')
    				score[x][0] = 'T';
    			if(score[x][0] == 'f')
    				score[x][0] = 'F';
    		}
    	
    	//Insert ID #'s
    	for(x=0; x<size; ++x)
    	{
    		cout<<"Please enter student "<<x+1<<"'s 4-digit ID number: ";
    		cin>>student[x];
    		while(student[x] > 9999 || student[x] < 1000)
    		{
    			cout<<"Please re-enter the student number: ";
    			cin>>student[x];
    		}
    	}
    	
    	//Insert Test Answers
    	for(x=1; x<size2; ++x)
    	{
    		cout<<"Please enter student "<<x<<"'s test scores(T/F):"<<endl;
    		for(int y=0; y<10; ++y)
    		{
    			cin>>score[y][x];
    			if(score[y][x] == 't')
    				score[y][x] = 'T';
    			if(score[y][x] == 'f')
    				score[y][x] = 'F';
    		}
    	}
    	
    	//Count correct Answers
    	for(x=1; x<size2; ++x)
    	{
    		for(int y=0; y<10; ++y)
    		{
    			if(score[y][0] == score[y][x])
    				++count[x-1];
    		}
    	}
    
    	//Sets Margin Left
    	cout<<setiosflags(ios::left);
    	
    	//Sets letter grades of all students
    	for(x=0; x<size; ++x)
    	{
    		if(count[x] == 10)
    			grade[x] = 'A';
    		else if(count[x] == 9)
    			grade[x] = 'B';
    		else if(count[x] == 8)
    			grade[x] = 'C';
    		else if(count[x] == 7)
    			grade[x] = 'C';
    		else if(count[x] == 6)
    			grade[x] = 'D';
    		else if(count[x] == 5)
    			grade[x] = 'D';
    		else
    			grade[x] = 'F';
    	}
    
    	system("cls");
    
    	//Print Key
    	cout<<setw(7)<<"Key";
    	for(int y=0; y<10; ++y)
    		cout<<score[y][0];
    
    ///////////////////////////////////////////////////////////////	HERE
    	//Prints results
    
    	for(x=0; x<size; ++x)
    	{
    		cout<<endl<<setw(7)<<student[x];
    		for(y=0; y<10; ++y)
    		{
    			if(score[y][0] != score[y][x])
    			{
    				Color_Red
    				cout<<score[y][x+1];
    			}
    			else if(score[y][0] == score[y][x])
    			{
    				Color_Green
    				cout<<score[y][x+1];
    			}
    			
    		}
    	//	Color_White
    		cout<<" # Correct: "<<setw(5)<<count[x]<<"Grade: "<<grade[x];
    	}
    	cout<<endl<<endl;
    
    ///////////////////////////////////////////////////////////////// TO HERE
    	//Class Average
    
    	double avg = 0;
    	for(x=0; x<size; ++x)
    	{
    		avg = avg + count[x];
    	}
    	avg = avg / size;
    	cout<<"Class Average: "<<avg<<" Correct"<<endl;
    
    	//Class Letter Average
    	char letter;
    	if(avg == 10)
    		letter = 'A';
    	else if(avg == 9)
    		letter = 'B';
    	else if(avg == 8)
    		letter= 'C';
    	else if(avg == 7)
    		letter = 'C';
    	else if(avg == 6)
    		letter = 'D';
    	else if(avg == 5)
    		letter = 'D';
    	else
    		letter = 'F';
    	cout<<"Letter Average: "<<letter<<endl<<endl;
    
    	return 0;
    }
    Last edited by GreenCherry; 10-24-2002 at 10:28 AM.
    I have a rabbit in my pants! Please be happy for me.

  2. #2
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    If your too lazy to make it yourself here is the source.(I can't make zips here)
    Last edited by GreenCherry; 12-18-2008 at 03:38 AM.
    I have a rabbit in my pants! Please be happy for me.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ...so what is your question, exactly? "How do I colour a section of text?" If so, this is compiler specific, you'll need to look in your documentation, or search the boards for previous posts of the same nature (there are porbably lots).

    Alternatively, you can post the name of your compiler, and someone may be able to help you..... but please try the searching first
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Errors When I Color

    Add cout.flush(); to the color defines and it will work.

    #define color_white SetTextAttributes(...); cout.flush();
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Thanks It Worked!
    Last edited by GreenCherry; 10-25-2002 at 01:44 PM.
    I have a rabbit in my pants! Please be happy for me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 04:11 PM
  2. Specific Character Text Color
    By TechWins in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2002, 04:42 AM
  3. Text and background Color
    By Goof Program in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-29-2002, 06:59 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21