SRL Archive - C/C++ Help and Tutorials

C++ Help.


Sin

2012-05-23 10:42:44

First off, i'm absolutely amazed and thankful at how similar Delphi/Pascal script is to C++, should make my learning transition much easier.
First off, I want to know how to do a case statement in C++ -


//Calculator
#include <iostream>
using namespace std;
int main()
{
//Defining variables
char cMenu; //Menu?
char cOperator; //Operator (+,-,*,/)
int Num1; //X so to speak, first integer
int Num2; //Y, second integer
int y;
int i;
i = 2;
y = 6;
while(i = 2)
{ //this is basically a begin while the while statement is a n IF statement.

cout<<"/ny First Calculator, G to calc, x to exit:"; //writeLn basically

cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu

if (cMenu == 104) //G I believe
{
cout<<"enter a value an operator then a value pressing enter each time to use this";
}
else if (cMenu == 120) //this is x I believe
{
break; //kills the procedure
}
else if (cMenu == 103)
{
cin>>Num1;
cin>>cOperator;
cin>>Num1;
}
else
{
cout<<"neither of the options were selected, try again!"
} }

}




as you can see, it looks a bit messy so I want to do a Simba-esque way (case cOperator of). Any help is appreciated!

Method

2012-05-23 10:56:35

Scroll down to the bottom of the page here for information about switch statements.

Also, I think you missed an '=' in your while loop predicate.

Sin

2012-05-23 13:40:57

Thanks! It's working now :)
+ rep.

Frement

2012-05-23 13:58:09

Also you have commented "kills the procedure" on break. Instead you should do:
return 0;

Sin

2012-05-23 19:50:41

Oo, would that make a difference? Or is it just for efficiency?

Sin

2012-05-24 10:08:31

Okay guys, new question!
How do I wait(500) in C++?
I attempted sleep(500) but this comes up -
38 G:\calculator.cpp `Sleep' undeclared (first use this function)

Kave

2012-05-24 15:32:09

From: Shay
Okay guys, new question!
How do I wait(500) in C++?
I attempted sleep(500) but this comes up -
38 G:\calculator.cpp `Sleep' undeclared (first use this function)


Check your includes.

Brandon

2012-05-24 15:41:40

It's case sensitive..

#include <Windows.h>

Sleep(500); //not sleep(500);

Sin

2012-05-24 15:41:58

I included windows.h and it's working fine now :)

My god, literally got ninja'd by you Brandon :p

zmon

2012-05-25 04:36:07

From: Shay
First off, I want to know how to do a case statement in C++


http://www-numi.fnal.gov/computing/d120/packages/WebDocs/HEAD/Companion/cxx_crib/switch.html

basically as they say - but you MUST use break statement else it will fall through

switch ( something ) {

case 1:
// Process for something = 1
...
break;

case 'n':
// process for 'n'

// oops we forgot break... not good because:
case 5 :
// Process for something = 5, or something = 'n' because we made a mistake and forgot to break
...
break;

default :
// Process for all other cases.
...

}

Sin

2012-05-25 06:03:07

Yeh I learned that yesterday, I found that it was showing all values instead of just one.
Also, can anyone tell me if itsnpossible to make a GUI type thing as the user interface?

Frement

2012-05-25 09:43:37

From: Shay
Yeh I learned that yesterday, I found that it was showing all values instead of just one.
Also, can anyone tell me if itsnpossible to make a GUI type thing as the user interface?


For GUI, I'd recommend Qt. Qt also simplifies a lot of other stuff. But first learn to code properly. Before you go making yourself a GUI, learn the basics and most of the more advanced stuff.

Sin

2012-05-25 09:56:57

Ah okay, what do you suggest I do next?

//Calculator
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
//Defining variables
char cMenu; //Menu?
char cOperator; //Operator (+,-,*,/)
int Num1; //X so to speak, first integer
int Num2; //Y, second integer
int y;
int i;
i = 2;
y = 6;
while(i = 2)
{ //this is basically a begin while the while statement is a n IF statement.

cout<<"First Calculator, g to calc, x to exit, h for help:"; //writeLn basically

cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu

if (cMenu == 104) //G I believe
{
cout<<"enter a value an operator then a value pressing enter each time to use this";
}
else if (cMenu == 120) //this is x I believe
{
return 0; //kills the procedure
}
else if (cMenu == 103)
{
cout<<"Enter first number: "<<endl;
cin>>Num1;
cout<<"Enter second number: "<<endl;
cin>>Num2;
cout<<"What action?"<<endl;
cin>>cOperator;
switch(cOperator) //case statement
{
case 43:
int ans; //declares "ans" as an integer;
ans = Num1 + Num2;
cout<<"If adding:"<<ans<<endl; //endL ends the line.
break;
case 45:
ans = Num1 - Num2;
cout<<"If subtracting: "<<ans<<endl;
break;
case 42:
ans = Num1 * Num2;
cout<<"If multiplying: "<<ans<<endl;
break;
case 47:
ans = Num1 / Num2;
cout<<"If dividing: "<<ans<<endl;
break;
}

}
else
{
cout<<"neither of the options were selected, try again!"<<endl;
}
}

}

masterBB

2012-05-25 10:00:19

Please use the correct code tags for readability. [.Highlight=c++] <- without .

//Calculator
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
//Defining variables
char cMenu; //Menu?
char cOperator; //Operator (+,-,*,/)
int Num1; //X so to speak, first integer
int Num2; //Y, second integer
int y;
int i;
i = 2;
y = 6;
while(i = 2)
{ //this is basically a begin while the while statement is a n IF statement.

cout<<"First Calculator, g to calc, x to exit, h for help:"; //writeLn basically

cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu

if (cMenu == 104) //G I believe
{
cout<<"enter a value an operator then a value pressing enter each time to use this";
}
else if (cMenu == 120) //this is x I believe
{
return 0; //kills the procedure
}
else if (cMenu == 103)
{
cout<<"Enter first number: "<<endl;
cin>>Num1;
cout<<"Enter second number: "<<endl;
cin>>Num2;
cout<<"What action?"<<endl;
cin>>cOperator;
switch(cOperator) //case statement
{
case 43:
int ans; //declares "ans" as an integer;
ans = Num1 + Num2;
cout<<"If adding:"<<ans<<endl; //endL ends the line.
break;
case 45:
ans = Num1 - Num2;
cout<<"If subtracting: "<<ans<<endl;
break;
case 42:
ans = Num1 * Num2;
cout<<"If multiplying: "<<ans<<endl;
break;
case 47:
ans = Num1 / Num2;
cout<<"If dividing: "<<ans<<endl;
break;
}

}
else
{
cout<<"neither of the options were selected, try again!"<<endl;
}
}

}

Sin

2012-05-25 10:02:24

From: masterBB
Please use the correct code tags for readability. [.Highlight=c++] <- without .

//Calculator
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
//Defining variables
char cMenu; //Menu?
char cOperator; //Operator (+,-,*,/)
int Num1; //X so to speak, first integer
int Num2; //Y, second integer
int y;
int i;
i = 2;
y = 6;
while(i = 2)
{ //this is basically a begin while the while statement is a n IF statement.

cout<<"First Calculator, g to calc, x to exit, h for help:"; //writeLn basically

cin>>cMenu; //what this does is that it asks for an input, and stores it in cmenu

if (cMenu == 104) //G I believe
{
cout<<"enter a value an operator then a value pressing enter each time to use this";
}
else if (cMenu == 120) //this is x I believe
{
return 0; //kills the procedure
}
else if (cMenu == 103)
{
cout<<"Enter first number: "<<endl;
cin>>Num1;
cout<<"Enter second number: "<<endl;
cin>>Num2;
cout<<"What action?"<<endl;
cin>>cOperator;
switch(cOperator) //case statement
{
case 43:
int ans; //declares "ans" as an integer;
ans = Num1 + Num2;
cout<<"If adding:"<<ans<<endl; //endL ends the line.
break;
case 45:
ans = Num1 - Num2;
cout<<"If subtracting: "<<ans<<endl;
break;
case 42:
ans = Num1 * Num2;
cout<<"If multiplying: "<<ans<<endl;
break;
case 47:
ans = Num1 / Num2;
cout<<"If dividing: "<<ans<<endl;
break;
}

}
else
{
cout<<"neither of the options were selected, try again!"<<endl;
}
}

}


Well my bad -.- I didn't know that existed..

masterBB

2012-05-25 10:06:13

From: Shay
Well my bad -.- I didn't know that existed..


Nobody does :P They should make a button for it or something ;) I forgive you.

Sin

2012-05-25 10:09:07

From: masterBB
Nobody does :P They should make a button for it or something ;) I forgive you.


Ty <3

Anyways, any suggestions about what I can make in C++ while on my spare period?

Main

2012-05-25 10:13:31

learn oop and you'll be set for uni.

Also how old are you sin?

You got a facebook? Cause am also from ontario (toronto).

masterBB

2012-05-25 10:13:54

I always like to make a jump in the deep. Try to look into directX 3D, I learned c++ with that :P, but most people will not recommend it, no idea why.

You will soon notice it isn't just that pascal and c++ are similar, but that java, pascal, c++, c, c#, actionscript, python and every other language works very similar. At a certain moment learning a new language is a matter of days instead of weeks/months.

Sin

2012-05-25 10:17:12

From: Main
learn oop and you'll be set for uni.

Also how old are you sin?

You got a facebook? Cause am also from ontario (toronto).


Im 15, and nah. I deactivated mine because I was getting addicted to it :p
And can you explain about OOP?

From: masterBB
I always like to make a jump in the deep. Try to look into directX 3D, I learned c++ with that :P, but most people will not recommend it, no idea why.

You will soon notice it isn't just that pascal and c++ are similar, but that java, pascal, c++, c, c#, actionscript, python and every other language works very similar. At a certain moment learning a new language is a matter of days instead of weeks/months.


This is definetly true. I was used to Java so I picked up Pascal in only a few days.
Also, what is D3D programming used for?

Recursive

2012-05-25 10:46:15

I think he meant to write loop instead of oop
And he's right cause in uni, they drilled us hard on those for loops and while loops. Plus also learn how to do a loop within a loop

EDIT: surfing forums on andriod ain't so bad

Main

2012-05-25 10:53:34

oop is object orientated programming.

Loops and stuff are quite easy if you have good logic from previous programming experience (such as simba hehe). Object orientate programming isn't too useful in terms of making a bot but its something you need for university maybe even beyond. So its a good to know :)

masterBB

2012-05-25 11:02:15

From: Chig
I think he meant to write loop instead of oop
And he's right cause in uni, they drilled us hard on those for loops and while loops. Plus also learn how to do a loop within a loop

EDIT: surfing forums on andriod ain't so bad


No, he meant oop. Not loop. Loops are easy and not important.

Objective oriented programming.

Let's say you got a wall. The wall exists out of bricks. Every brick is from the brick class.

Pseudocode:

class Brick{
var width, height:Int;
var x, y:Int;


constructor(_x, _y, _w, _h:Int){
this.create();
x = _x;
y = _y;
width = _w;
height = _h;
}

Draw(canvas:Canvas){
canvas.drawThis(x, y, width, height);
}
}

Pseudocode:

class Wall{
var bricks:Array;


Create(){
var i:Int = 0;
var j:Int = 0;

for(j = 0; j < 10; j++) {
for(i = 0; i < 10; i++) {
bricks.push(new Brick(5*j,10*i,5,10));
}
}
}

Draw(canvas:Canvas){
for(j = 0; j < bricks.length; j++) {
bricks[j].draw(this);
}
}
}

edit:
If you want to start with directX look into this: http://drunkenhyena.com/cgi-bin/directx.pl