Two C++ Qs

Questions about programming languages and debugging
Post Reply
User avatar
Asuna
suck-o-fied!
suck-o-fied!
Posts: 92
Joined: 27 Dec 2006, 17:00
17
Location: New York, USA

Two C++ Qs

Post by Asuna »

OK, two c++ questions!

Question 1:

I know that you can clear a screen in Q-Basic with:

Code: Select all

CLS
but... after looking through the tut for C++ that shamir gave me, I can't seem to find anyway of clearing the screen. Is there a command to clear the screen at all?

See, what I'm trying to do is to have a Main Menu (which is only syntax, no pictures (yet...) and when you enter the number to start a new game, it clears the screen and brings up the character creation screen (again, will only be syntax now...).

Question 2:

How do you get music to play as background music. I have a file that I want to play when you're on the Main Menu (title.mid), but how do I do that?

Here is what I tried:

Code: Select all

#include <iostream>

#define tmusic 'title.mid'

int main ()
{
cout << tmusic
return 0;
}
But that doesn't work (I got the idea from my short experience with DM (that Build Your Own Net Dream (BYOND) code)). I don't think the tut had anything on sounds (the only sound I can make is the alert beep ("\a"). And is there a way to have it loop until you goto the character creation screen?

maybe:

Code: Select all

int tmusic ()
{
while (curmenu == 1) {
//then the code to play the music (whatever it is)
}
}

shamir
Computer Manager
Computer Manager
Posts: 853
Joined: 01 Mar 2007, 17:00
17
Location: NY
Contact:

Post by shamir »

is it sops to be a hm?

Code: Select all

# include <iostream.d> 

User avatar
Asuna
suck-o-fied!
suck-o-fied!
Posts: 92
Joined: 27 Dec 2006, 17:00
17
Location: New York, USA

Post by Asuna »

shamir wrote:is it sops to be a hm?

Code: Select all

# include <iostream.d> 
I don't really understand what you posted (srry :oops: )

sops = ?
hm = ?

shamir
Computer Manager
Computer Manager
Posts: 853
Joined: 01 Mar 2007, 17:00
17
Location: NY
Contact:

Post by shamir »

never mind sorry dude :lol:

User avatar
Asuna
suck-o-fied!
suck-o-fied!
Posts: 92
Joined: 27 Dec 2006, 17:00
17
Location: New York, USA

Post by Asuna »

Don't worry about it.

Do you know of a way to have music play in the program and how to clear the screen?

User avatar
Stavros
ΜΟΛΩΝ ΛΑΒΕ
ΜΟΛΩΝ ΛΑΒΕ
Posts: 1098
Joined: 02 Jan 2006, 17:00
18
Location: Mississippi, U.S.A.

Post by Stavros »

Alright, well I can answer Question 1 and it's really simple:

Code: Select all

system("cls");
You are welcome. I'm not sure how to make music play. I'll ask one of my friends. I think one of them should know.

User avatar
Swan
Knight of the Sword
Knight of the Sword
Posts: 827
Joined: 18 Oct 2006, 16:00
17
Contact:

Post by Swan »

Just as some general points:

Think very carefully about your variables. You wanna be able to pinpoint exactly (or thereabouts) where your errors are coming from, so think about using static local variables as opposed to global variables.

Although there is nothing wrong with global variables per se, the fact that they are used throughout the whole of your program can make it akward to identify where they are causing the problem.

Static variables are easy and their syntax is as follows:

static (data type) variable name;

so

static int grades;

If your learning C++ for the first can i make a suggestion? Try Visual Studio 2005 its fantastic. Literal strings are colured red, code words in blue, and you dont need to concern yourself with messing around with the function headers, you can omit the .h ;)

One final thing that springs to mind that may cause problems, is remember that because C++ is a logical program, and thus reads functions from top to bottom, you must actually declare functions after the main function.

So, every C++ program MUST have a int main (void) function, else it wont work. However, if you try and declare a function before the main function you will get nasty errors. This is true of all C++ compilers.

So, to beat this, use "prototyping". This simple means you declare the function or functions along with the header files.

simply declare the function with the header files, and include a semicolon at the end. :wink:

This just intended to save you work in the long run, if you can get to grips of those bits of C++ you will find your work a helluva of a lot easier.

[/code]

User avatar
Asuna
suck-o-fied!
suck-o-fied!
Posts: 92
Joined: 27 Dec 2006, 17:00
17
Location: New York, USA

Post by Asuna »

i'm not too sure that i declare other functions after the main function if it reads top to bottom. if you want to call a function in your main function, unless it was declared above the main function, it would not know what function you are calling. heres an example.

wrong way

Code: Select all

int main ()
{
     \\main function shit
      int z
      z = funct2 (5,3);
      return 0;
}

int funct2 (int a, int b)
{
       int r;
       r=a+b;
       return (r);
}
right way

Code: Select all

int funct2 (int a, int b)
{
       int r;
       r=a+b;
       return (r);
}

int main ()
{
     \\main function shit
      int z
      z = funct2 (5,3);
      return 0;
}

User avatar
Stavros
ΜΟΛΩΝ ΛΑΒΕ
ΜΟΛΩΝ ΛΑΒΕ
Posts: 1098
Joined: 02 Jan 2006, 17:00
18
Location: Mississippi, U.S.A.

Post by Stavros »

You have that backwards, Asuna.

I was taught that the main() function is always the top function. All other functions (no matter what order) go below.

Static variables (variables that the whole function can read) go above main() and header files go above the world variables.

A visual example:

Code: Select all

#include <iostream> //Header

using namespace std;

char char1 = " "; //"World Variable" (I forget what my teacher called it)

int main()
{
//Insert main function of program here
}

int funct1()
{
//Insert [optional] fuction here
}

...

User avatar
Asuna
suck-o-fied!
suck-o-fied!
Posts: 92
Joined: 27 Dec 2006, 17:00
17
Location: New York, USA

Post by Asuna »

really? the tutorial i'm reading has this:

Code: Select all

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
  return 0;
}
the url: http://www.cplusplus.com/doc/tutorial/functions.html

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

the other way is to do

Code: Select all


int funct1(int a, int b);

int main()
{
   std::cout << funct1(3,4);
}

int funct1(int a, int b)
{
   return a + b;
}


Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

Post Reply