Jump to content
  • Advertisement
Sign in to follow this  
spacekid434

Please Help.

This topic is 2605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone please help me. I am new to c++ and I am trying to make a program that displays text character by character with a Sleep() in between while it plays sound in the background (Portal's: Still Alive).

I have that part done but I have also included formatting which allows you to drop down a line, new paragraph, wait a certain amount of time.

// ' == new line "\n"
// '%' == New paragraph "\n\n"
// '#' == Wait 3 seconds Sleep(3000)
// '^' == wait 2.5 seconds Sleep(2500)
// '&' == wait 2 seconds Sleep(2000)
// '+' == wait 1.5 seconds Sleep(1500)
// '-' == wait 1 second Sleep(1000)
// '~' == wait 100ms Sleep(100)
// '[' == comma & small second wait "," & Sleep(250)
// ']' == Half second wait with no "," Sleep(250)
//
// '_' + (NEW TEMPO up to 4 chars) e.g. "_0120". REMEMBER: The smaller the number, the faster the speed.
//
// TO USE A SYMBOL IN THE ABOVE LIST, PRECEED IT WITH '/'. THE CHAR AFTER WILL BE DISPLAYED
//====================================================================


The problem that I am having, is that I have tried adding a formatting command to change how quickly the characters display (The "_0120" above) and i cant get my parser to change the tempo at all, and when I cout << tempo; it tells some number in the 5000's (e.g. 53438, 53348).

Can someone please have a look at my code and tell where I am going wrong.

p.s. I wrote this code with the intention of it being easily reusable, thats why there are a lot of comments and #defines etc.

main.cpp:
//Still Alive

#include<iostream>
#include<string>
#include<Windows.h>
#include"resource.h"
#include"lyrics.h"
#include"parse.h"

#pragma comment(lib,"Winmm.lib")

using std::cout;
using std::endl;
using std::string;

int main(){

system("title FORM-29827281-12");
system("color E");
cout << "Forms FORM-29827281-12:" << endl;
cout << "Test Assessment Report\n\n";

PlaySound(TEXT("StillAlive.wav"), NULL, SND_ASYNC);

YCTparse::parse();

PlaySound(NULL, 0, 0);
system("pause");
return 0;
}


resource.h:
//resources for Parser

#ifndef _RESOURCE_
#define _RESOURCE_

#define DEBUG true

//====================================================================
//
// purpose: Definitions for parsers use.
//
//
// ' == new line "\n"
// '%' == New paragraph "\n\n"
// '#' == Wait 3 seconds Sleep(3000)
// '^' == wait 2.5 seconds Sleep(2500)
// '&' == wait 2 seconds Sleep(2000)
// '+' == wait 1.5 seconds Sleep(1500)
// '-' == wait 1 second Sleep(1000)
// '~' == wait 100ms Sleep(100)
// '[' == comma & small second wait "," & Sleep(250)
// ']' == Half second wait with no "," Sleep(250)
//
// '_' + (NEW TEMPO up to 4 chars) e.g. "_0120". REMEMBER: The smaller the number, the faster the speed.
//
// TO USE A SYMBOL IN THE ABOVE LIST, PRECEED IT WITH '/'. THE CHAR AFTER WILL BE DISPLAYED
//====================================================================

#define NEW_LINE '
#define WAIT_THREE '#'
#define WAIT_TWO_HALF '^'
#define WAIT_TWO '&'
#define WAIT_ONE_HALF '+'
#define WAIT_ONE '-'
#define WAIT_GROUP '~'
#define COMMA_WAIT '['
#define COMMA_NONE ']'
#define NEW_PARAGRAPH '%'
#define COLOR_SET '?' //Char that descibes a foreground color change
#define IGNORE_COMMAND '/'
#define CHANGE_TEMPO '_' //Changes the tempo to a 4 char number after

#define NUM_COL_CHAR 3 //Number of characters after to describe color.
#define WAIT_THREE_TIME 3000
#define WAIT_TWO_HALF_TIME 2500
#define WAIT_TWO_TIME 2000
#define WAIT_ONE_HALF_TIME 1500
#define WAIT_ONE_TIME 1000
#define WAIT_GROUP_TIME 100
#define COMMA_WAIT_TIME 250

//color codes: max chars per code = NUM_COL_CHAR
#define BLUE "blu"
#define GREEN "gre"
#define AQUA "aqu"
#define RED "red"
#define PURPLE "pur"
#define YELLOW "yel"
#define WHITE "whi"
//end of color code

#define BASE_TIME 100 //time in MS between parses

#endif


lyrics.h:
//Lyrics for program to display

#ifndef _LYRICS_
#define _LYRICS_

#include<string>
#include"resource.h"

using namespace std;

//====================================================================
//
//purpose: Stores the array with the lyrics.
//
//
// ' == new line "\n"
// '%' == New paragraph "\n\n"
// '#' == Wait 3 seconds Sleep(3000)
// '^' == wait 2.5 seconds Sleep(2500)
// '&' == wait 2 seconds Sleep(2000)
// '+' == wait 1.5 seconds Sleep(1500)
// '-' == wait 1 second Sleep(1000)
// '~' == wait 100ms Sleep(100)
// '[' == comma & small second wait "," & Sleep(250)
// ']' == Half second wait with no "," Sleep(250)
//
// '_' + (NEW TEMPO up to 4 chars) e.g. "_0120". REMEMBER: The smaller the number, the faster the speed.
//
// TO USE A SYMBOL IN THE ABOVE LIST, PRECEED IT WITH '/'. THE CHAR AFTER WILL BE DISPLAYED
//====================================================================

std::string lyrics = "_0100This was a triumph$^Im making a note hereHUGE SUCCESS'$-Its hard to overstate my satisfaction%#~~~~$Aperture Science {:content:}amp;We do what we must ]because ]we can$+_0110For the good] of all of us exept the ones who are dead%_0020But theres no sense crying over _0010every mistake$You just keep on trying till you run out of CAKE.$and the science gets done.$And you make a neat gun.$For the people who are still alive!";

const unsigned int LYRICS_SIZE = lyrics.size();

#endif


parse.h: [color="#00ff00"]WHERE THE PROBLEM PROBABLY IS
//The parser functions and #define

#ifndef _PARSER_
#define _PARSER_

#include<iostream>
#include<string>
#include<Windows.h>
#include"lyrics.h"
#include"resource.h"

using std::cout;
using std::endl;
using std::string;

//====================================================================
//
//purpose: contains functions, vars and #defines releated to the string parser
//
//
// ' == new line "\n"
// '%' == New paragraph "\n\n"
// '#' == Wait 3 seconds Sleep(3000)
// '^' == wait 2.5 seconds Sleep(2500)
// '&' == wait 2 seconds Sleep(2000)
// '+' == wait 1.5 seconds Sleep(1500)
// '-' == wait 1 second Sleep(1000)
// '~' == wait 100ms Sleep(100)
// '[' == comma & small second wait "," & Sleep(250)
// ']' == Half second wait with no "," Sleep(250)
//
// '_' + (NEW TEMPO up to 4 chars) e.g. "_0120". REMEMBER: The smaller the number, the faster the speed.
//
// TO USE A SYMBOL IN THE ABOVE LIST, PRECEED IT WITH '/'. THE CHAR AFTER WILL BE DISPLAYED
//====================================================================

namespace YCTparse{
void parse(){
bool useBase = true;
unsigned int tempo;

for(unsigned int i = 0; i < LYRICS_SIZE; ++i){ //The Parser loop
if(useBase == true){
tempo = BASE_TIME;
}
if(lyrics == NEW_LINE){
cout << "\n";
}
else if(lyrics == NEW_PARAGRAPH){
cout << "\n\n";
}
else if(lyrics == WAIT_THREE){
Sleep(WAIT_THREE_TIME);
}
else if(lyrics == WAIT_TWO_HALF){
Sleep(WAIT_TWO_HALF_TIME);
}
else if(lyrics == WAIT_TWO){
Sleep(WAIT_TWO_TIME);
}
else if(lyrics == WAIT_ONE_HALF){
Sleep(WAIT_ONE_HALF_TIME);
}
else if(lyrics == WAIT_ONE){
Sleep(WAIT_ONE_TIME);
}
else if(lyrics == WAIT_GROUP){
Sleep(WAIT_GROUP_TIME);
}
else if(lyrics == COMMA_WAIT){
cout << ',';
Sleep(COMMA_WAIT_TIME);
}
else if(lyrics == COMMA_NONE){
Sleep(COMMA_WAIT_TIME);
}
else if(lyrics == IGNORE_COMMAND){
char charDisplay = lyrics[(i + 1)];
cout << charDisplay;
i++;
}
else if(lyrics == COLOR_SET){
string colValue = "" + lyrics[(i + 1)] + lyrics[(i + 2)] + lyrics[(i + 3)];

if(colValue == BLUE){
system("color 1");
}
if(colValue == GREEN){
system("color 2");
}
if(colValue == AQUA){
system("color 3");
}
if(colValue == RED){
system("color 4");
}
if(colValue == PURPLE){
system("color 5");
}
if(colValue == YELLOW){
system("color 6");
}
if(colValue == WHITE){
system("color 7");
}
}
//=================================
//PROBLEM STARTS HERE.
//=================================
else if(lyrics == CHANGE_TEMPO){
unsigned int newTempo = BASE_TIME;

int Lfirst = static_cast<int>(lyrics[i + 1]);
int Lsecond = static_cast<int>(lyrics[i + 2]);
int Lthird = static_cast<int>(lyrics[i + 3]);
int Lfourth = static_cast<int>(lyrics[i + 4]);

if(Lfirst == 0){
if(Lsecond == 0){
if(Lthird == 0){
if(Lthird == 0){
newTempo = BASE_TIME;
}
else{
newTempo = Lfourth;
}
}
else{
newTempo = (Lthird * 10) + Lfourth;
}
}
else{
newTempo = (Lsecond * 100) + (Lthird * 10) + Lfourth;
}
}
else{
newTempo = (Lfirst * 1000) + (Lsecond * 100) + (Lthird * 10) + Lfourth;
}

tempo = newTempo;
i += 4;

if(DEBUG == true){cout << tempo << endl;}
}
else{
cout << lyrics;
Sleep(tempo);
}
//=================================
//PROBLEM PROBABLY ENDS HERE.
//=================================
}
cout << "\n\n";
}
}

#endif




Thanks

Share this post


Link to post
Share on other sites
Advertisement
You are using the character values, not the numeric values. '0' != 0 and '9' != 9. A quick way of converting them is to subtract the value of the character constant '0' from each character. This assumes that the numeric characters are contiguous and ascending (which they almost always are).

Also you can clean up your conditionals like so:

if(Lfirst || LSecond || Lthird || Lfourth)
{
tempo = (Lfirst * 1000) + (Lsecond * 100) + (Lthird * 10) + Lfourth;
}
else
{
tempo = BASE_TIME;
}

Or:

tempo = (Lfirst * 1000) + (Lsecond * 100) + (Lthird * 10) + Lfourth;

if(tempo == 0)
{
tempo = BASE_TIME;
}

There is no point checking for each component is zero to omit it from the equation, evaluating the full equation with the zeros has the same outcome.

Share this post


Link to post
Share on other sites
It looks like you're using the ascii values instead of the numerical values.

The string stores the ascii character representation of the numbers, which when cast to an integer, isn't the same as the value it represents.

For example, the number '0' has ascii value '60' and the number '6' has the ascii value '66'.

You could possibly get away with using the ascii value you're already using subtracting 60.

Possible solution:

int Lfirst = static_cast(lyrics[i + 1])-60;
int Lsecond = static_cast(lyrics[i + 2])-60;
int Lthird = static_cast(lyrics[i + 3])-60;
int Lfourth = static_cast(lyrics[i + 4])-60;


I don't recommend using this method however, there's probably a more robust solution.

Share this post


Link to post
Share on other sites
The char '5' has an int value of 53 as shown in the ascii chart: http://www.automatio...com/logic-ascii

By the looks of the code, you were expecting that casting the char to an int, you were expecting '5' to become the int value of 5.

int Lfirst = static_cast<int>(lyrics[i + 1]);
int Lsecond = static_cast<int>(lyrics[i + 2]);
int Lthird = static_cast<int>(lyrics[i + 3]);
int Lfourth = static_cast<int>(lyrics[i + 4]);


Use:

int Lfirst = static_cast<int>(lyrics[i + 1]) - static_cast<int>('0');
int Lsecond = static_cast<int>(lyrics[i + 2]) - static_cast<int>('0');
int Lthird = static_cast<int>(lyrics[i + 3]) - static_cast<int>('0');
int Lfourth = static_cast<int>(lyrics[i + 4]) - static_cast<int>('0');


Not a 'proper'' fix but it will work for the moment.

Edit Ninja'ed

Share this post


Link to post
Share on other sites

The char '5' has an int value of 53 as shown in the ascii chart: http://www.automatio...com/logic-ascii

By the looks of the code, you were expecting that casting the char to an int, you were expecting '5' to become the int value of 5.

int Lfirst = static_cast<int>(lyrics[i + 1]);
int Lsecond = static_cast<int>(lyrics[i + 2]);
int Lthird = static_cast<int>(lyrics[i + 3]);
int Lfourth = static_cast<int>(lyrics[i + 4]);


Use:

int Lfirst = static_cast<int>(lyrics[i + 1]) - static_cast<int>('0');
int Lsecond = static_cast<int>(lyrics[i + 2]) - static_cast<int>('0');
int Lthird = static_cast<int>(lyrics[i + 3]) - static_cast<int>('0');
int Lfourth = static_cast<int>(lyrics[i + 4]) - static_cast<int>('0');


Not a 'proper'' fix but it will work for the moment.

Edit Ninja'ed


I must've got an incorrect ascii chart, or I looked at the wrong column... but yeah, that's a better solution.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!