C++ For Beginners


monstercameron

Well-Known Member
Joined
Oct 19, 2010
Messages
1,001
Website
www.thinkteletronics.com
lets use this post so we can post code and get other members to help us troubleshoot.

i am practicing some c++ before i attempt to start game programming. Any tips,tricks,advice or anything positive would be appreciated

Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>


using namespace std;

int primero;
int segundo;
string operation;
string whatnext;
int active;

int main()
{
    while(active == 1)
    {
        cout << "Hello world!" << endl;
        cout << "Welcome to Calc++" << endl;
        cout << "enter unit" << endl;
        cin >> primero ;
        cout << "enter operation" << endl;
        cin >> operation ;
        cout << "enter unit" << endl;
        cin >> segundo ;

        if (operation == "+")
            cout << primero + segundo << endl;
        if (operation == "-")
            cout << primero - segundo << endl;
        if (operation == "x")
            cout << primero * segundo << endl;
        if (operation == "/")
            cout << primero / segundo << endl;

        cout << "what's next" << endl;
        cin >> whatnext;

        if (whatnext = "quit")
            active = 0;

        system("pause");
        system("clear");
    }
    return 0;
}
 
code revised 1

Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>


using namespace std;

int primero;
int segundo;
string operation;
string whatnext;
int active = 1;

int main()
{
    while(active == 1)
    {
        cout << "Hello world!" << endl;
        cout << "Welcome to Calc++" << endl;
        cout << "enter unit ........ ";
        cin >> primero ;
        cout << endl << "enter operation ... ";
        cin >> operation ;
        cout << endl << "enter unit ........ ";
        cin >> segundo ;
        cout << endl << "Your answer = ..... ";
        if (operation == "+")
            cout << primero + segundo << endl;
        if (operation == "-")
            cout << primero - segundo << endl;
        if (operation == "x")
            cout << primero * segundo << endl;
        if (operation == "/")
            cout << primero / segundo << endl;

        cout << endl << "what's next ....... ";
        cin >> whatnext;

        if (whatnext == "quit")
            active = 0;

        system("clear");
    }
    return 0;
}
 
rev2
problems: after whats next -for entering operation- it doesn't work

Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>


using namespace std;

int primero;
int segundo;
string operation;
string whatnext;
int active = 1;
int answer;
int newanswer;
int otravez;

int main()
{
    while(active == 1)
    {
        cout << "Hello world!" << endl;
        cout << "Welcome to Calc++" << endl;
        cout << "enter unit ........ ";
        cin >> primero ;
        cout << endl << "enter operation ... ";
        cin >> operation ;
        cout << endl << "enter unit ........ ";
        cin >> segundo ;

        if (operation == "+")
            answer = primero + segundo ;
        if (operation == "-")
            answer = primero - segundo ;
        if (operation == "x")
            answer = primero * segundo ;
        if (operation == "/")
            answer = primero / segundo ;

        cout << endl << "Your answer = ..... " << answer ;

        cout << endl << "what's next ....... ";
        cin >> whatnext;

        if (whatnext == "quit")
            active = 0;
        if (whatnext == "+")
            cout << endl << "enter unit ........ ";
            cin >> otravez;
            newanswer = answer + otravez ;
        if (whatnext == "-")
            cout << endl << "enter unit ........ ";
            cin >> otravez;
           newanswer = answer - otravez ;
        if (whatnext == "x")
            cout << endl << "enter unit ........ ";
            cin >> otravez;
            newanswer = answer * otravez ;
        if (whatnext == "/")
            cout << endl << "enter unit ........ ";
            cin >> otravez;
           newanswer = answer / otravez ;

        answer = newanswer;

        cout << endl << "Your answer = ..... " << answer ;
        cout << endl << "what's next ....... ";
        cin >> whatnext;

        system("clear");
    }
    return 0;
}
 
rev3
fix: now loops in whats next
problems: for additional operations after whats next

Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>


using namespace std;

int primero;
int segundo;
string operation;
string whatnext;
int active = 1;
int answer;
int newanswer;
int otravez;

int main()
{
    while(active == 1)
    {
        cout << "Hello world!" << endl;
        cout << "Welcome to Calc++" << endl;
        cout << "enter unit ........ ";
        cin >> primero ;
        cout << endl << "enter operation ... ";
        cin >> operation ;
        cout << endl << "enter unit ........ ";
        cin >> segundo ;

        if (operation == "+")
            answer = primero + segundo ;
        if (operation == "-")
            answer = primero - segundo ;
        if (operation == "x")
            answer = primero * segundo ;
        if (operation == "/")
            answer = primero / segundo ;

        cout << endl << "Your answer = ..... " << answer ;

        cout << endl << "what's next ....... ";
        cin >> whatnext;

        while(whatnext != "quit")
        {
            if (whatnext == "quit")
                active = 0;
            if (whatnext == "+")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
                newanswer = answer + otravez ;
            if (whatnext == "-")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
               newanswer = answer - otravez ;
            if (whatnext == "x")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
                newanswer = answer * otravez ;
            if (whatnext == "/")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
               newanswer = answer / otravez ;

            answer = newanswer;

            cout << endl << "Your answer = ..... " << answer ;
            cout << endl << "what's next ....... ";
            cin >> whatnext;
        }
        system("clear");
    }
    return 0;
}
 
rev4
added: clear to whats next
problem: other ops are still acting weird!

Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>


using namespace std;

int primero;
int segundo;
string operation;
string whatnext;
int active = 1;
int answer;
int newanswer;
int otravez;

int main()
{
    while(active == 1)
    {
        cout << "Hello world!" << endl;
        cout << "Welcome to Calc++" << endl;
        cout << "enter unit ........ ";
        cin >> primero ;
        cout << endl << "enter operation ... ";
        cin >> operation ;
        cout << endl << "enter unit ........ ";
        cin >> segundo ;

        if (operation == "+")
            answer = primero + segundo ;
        if (operation == "-")
            answer = primero - segundo ;
        if (operation == "x")
            answer = primero * segundo ;
        if (operation == "/")
            answer = primero / segundo ;

        cout << endl << "Your answer = ..... " << answer ;
        cout << endl << "clear, +,-,x,/, quit";
        cout << endl << "what's next ....... ";
        cin >> whatnext;

        while(whatnext != "quit")
        {
            if (whatnext == "quit")
                active = 0;
            if (whatnext == "clear")
                break;
            if (whatnext == "+")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
                newanswer = answer + otravez ;
            /*if (whatnext == "-")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
               newanswer = answer - otravez ;
            if (whatnext == "x")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
                newanswer = answer * otravez ;
            if (whatnext == "/")
                cout << endl << "enter unit ........ ";
                cin >> otravez;
               newanswer = answer / otravez ;*/

            answer = newanswer;

            cout << endl << "Your answer = ..... " << answer ;
            cout << endl << "clear, +,-,x,/, quit";
            cout << endl << "what's next ....... ";
            cin >> whatnext;
        }
        system("clear");
    }
    return 0;
}
 
hint: use braces to encapsulate your if blocks.

c++ is not like python where the indent is important, {} are the key.
 
Code:
const int NumSprites = 30;
SDL_Surface* spriteSurfaces[ NumSprites ];
char filename[ 100 ];
for( int i = 0; i < NumSprites; ++i ) {
    snprintf( filename, sizeof( filename ), "sprite%02d.bmp", i );
    spriteSurfaces[i] = SDL_LoadBMP( filename );
}
 
i need some help...im kinda stuck.
these are a bunch of functions i have been using to learn file i/o(I WANT IT TO EVOLVE INTO A INTERPRETER FOR MY OWN LANGUAGE).
please dont rip me a new one for my noob questions!!!

there are a few things i dont understand:
1. what are all teh includes for?
2. what does namespace std mean?
3. @ func opentext(that i am trying to type in the name of the text file) is this the correct way to structure the function
4. is there an easy way to convert/switch between variable types...like javascript/python?
5. is it a good idea to have all teh functions in an header file?
6. after running thru func runmetest() and saving a teh variable statement to my file (book.eng) it only saves the first word in teh string. anyone understand what happened?

Code:
#ifndef RUNTIME_H_INCLUDED
#define RUNTIME_H_INCLUDED
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <fstream>
//#include "english.config"
using namespace std;

int running = 1;
string motd = "Welcome To My Language Interpreter...English!";
string CUSinput;
string help = "not yrt integrated";
string version = "0.0.1";
string currentkey;
string statement;
string url;
ofstream myfile;

int callouts()
{
    if (CUSinput == "motd")
        cout << endl << motd;
    if (CUSinput == "version")
        cout << endl << version;
    /*
    if (CUSinput == "time")
        cout << endl << "the time is now:  " << ctime;
    */
    if (CUSinput == "help")
        cout << endl << help;
    if (CUSinput == "open file")
    {
        cout << "tell me what to open:  ";
        cin >> opentext();
    }
    return 0;
}
/*int keypress()
{}
*/
int opentext(string url)
{
myfile.open (url);
cout << "   opening...";
myfile.close();
return 0;
}
int runmetest()
{
    if (CUSinput == "statement")
    {
        cout << "tell me what to save:  ";
        cin >> statement;
        //ofstream myfile;
        //myfile.open ("book.eng");
        cout << statement << "   saving...";
        myfile << statement;
        myfile.close();
    }

  return 0;
}

#endif // RUNTIME_H_INCLUDED
 
found this for opentext() converts one regular string to a string needed for myfile.open.

Code:
string opentext(string url)
{
    myfile.open (url.c_str());
    cout << "   opening...";
    myfile.close();
    return 0;
}

hey can i do this
Code:
string opentextnow = opentext();
 
work around no issues compiling... but how do i:
1. add a line and not rewrite the whole file
...

Code:
int runmetest2()
{
    if (CUSinput == "statement")
    {
        cout << "tell me what to open:  ";
        cin >> url;
        cout << "tell me what to save:  ";
        cin >> statement;
        ofstream myfile;
        myfile.open (url.c_str());
        cout << "   opening...";
        cout << statement << "   saving...";
        myfile << statement;
        myfile.close();
    }
    return 0;
}
 
works until cin...tehn blank...but teh rest of the program still works.
any thoughts???

Code:
int readmetest()
{
    if (CUSinput == "open")
    {
        cout << "tell me what to open:  ";
        cin >> url;
        ifstream myfileread;
        myfileread.open (url.c_str());
        cout << "   opening...";
        while(!myfileread.eof())
            {
                getline (myfileread,MYbook);
                cout << MYbook << endl;
            }
        myfileread.close();
	}
    return 0;
}

edit:
got it working
Code:
int readmetest()
{
    if (CUSinput == "open")
    {
        cout << "tell me what to open:  ";
        cin >> url;
        ifstream myfileread;
        myfileread.open (url.c_str());
        cout << "   opening...";
        while(!myfileread.eof())
            {
                getline (myfileread,MYbook);
                while(whatnext == "yes")
                    {
                        cout << MYbook << endl;
                        cin >> whatnext;
                    }
                    whatnext = "yes";
            }
        myfileread.close();
	}
    return 0;
}
 
You still need answers to your earlier questions?

FYI, once you really want to use SDL work through these tutorials: http://www.lazyfoo.net/SDL_tutorials/
 
Pickle said:
You still need answers to your earlier questions?

FYI, once you really want to use SDL work through these tutorials: http://www.lazyfoo.net/SDL_tutorials/

i have to go to work and only have a small window for programming and what im doing is going through as much code as i can...plus whenever other noobs are starting and end up with these errors they can see how i did it...as for sdl im stuck at color keying on lazyfoo.
but answers for previous questions would still be good!
peace!
 
Last edited by a moderator:
hmn said:
Code:
const int NumSprites = 30;
SDL_Surface* spriteSurfaces[ NumSprites ];
char filename[ 100 ];
for( int i = 0; i < NumSprites; ++i ) {
    snprintf( filename, sizeof( filename ), "sprite%02d.bmp", i );
    spriteSurfaces[i] = SDL_LoadBMP( filename );
}
could you elaborate on how to use this codeblock, and whats with the arrays?
 
Last edited by a moderator:
teh entire program!
just rushing! any comments would make my day(or night)!

Code:
#include "runtime.h"

int main()
{
    while(running == 1)
    {
        cout << endl << motd << endl;
        callouts();
        cin >> CUSinput;
        //runmetest();
        runmetest2();
        readmetest();
    }
    return 0;
}
Code:
#ifndef RUNTIME_H_INCLUDED
#define RUNTIME_H_INCLUDED
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <fstream>
//#include "english.config"
using namespace std;

int running = 1;
string motd = "Welcome To My Language Interpreter...English!";
string CUSinput;
string help = "not yrt integrated";
string version = "0.0.1";
string currentkey;
string statement;
string url;
ofstream myfile;
string opentextnow;
string MYbook;
string whatnext = "yes";

int callouts()
{
    if (CUSinput == "motd")
        cout << endl << motd;
    if (CUSinput == "version")
        cout << endl << version;
    /*
    if (CUSinput == "time")
        cout << endl << "the time is now:  " << ctime;
    */
    if (CUSinput == "help")
        cout << endl << help;
    system("clear");
    return 0;
}

/*
int opentext(string url)
{
    myfile.open (url.c_str());
    cout << "   opening...";
    myfile.close();
    return 0;
}
*/
/*
int runmetest()
{
    if (CUSinput == "statement")
    {
        cout << "tell me what to save:  ";
        cin >> statement;
        //ofstream myfile;
        //myfile.open ("book.eng");
        cout << statement << "   saving...";
        myfile << statement;
        myfile.close();
    }
    return 0;
}
*/

int runmetest2()
{
    if (CUSinput == "statement")
    {
        cout << "tell me what to open:  ";
        cin >> url;
        cout << "tell me what to save:  ";
        cin >> statement;
        ofstream myfile;
        myfile.open (url.c_str());
        cout << "   opening...";
        cout << statement << "   saving...";
        myfile << statement;
        myfile.close();
    }
    return 0;
}

int readmetest()
{
    if (CUSinput == "open")
    {
        cout << "tell me what to open:  ";
        cin >> url;
        ifstream myfileread;
        myfileread.open (url.c_str());
        cout << "   opening...";
        while(!myfileread.eof())
            {
                getline (myfileread,MYbook);
                while(whatnext == "yes")
                    {
                        cout << MYbook << endl;
                        cin >> whatnext;
                    }
                    whatnext = "yes";
            }
        myfileread.close();
	}
    return 0;
}






#endif // RUNTIME_H_INCLUDED

eventually i want to parse this text!

Code:
start.
say "hello world".
wait for keypress(return).
the end.
 
monstercameron said:
1. what are all teh includes for?
2. what does namespace std mean?
3. @ func opentext(that i am trying to type in the name of the text file) is this the correct way to structure the function
4. is there an easy way to convert/switch between variable types...like javascript/python?
5. is it a good idea to have all teh functions in an header file?
6. after running thru func runmetest() and saving a teh variable statement to my file (book.eng) it only saves the first word in teh string. anyone understand wha

string to int or int to string
In c++ i use a stringstream

Code:
i_to_a( int num)
{
string str;
stringstream ss;

ss << num;
ss >> str;

return str;
}

1. Includes are needed to define things you use, most of you have is for file i/o. (didnt see what you needed ctime.h for)
2. Namespace is basically a way to sub divide the scopes. For example string is actually defined as std::string, i could for example create my own namespace with a type string pickle::string. Since each string is defined for a specific namespace, the compiler knows which one to use. When you use the line use namespace std then the compiler automatically assumes everything in your application is in the scope of std. So you can write string without the std::. More info http://www.cplusplus.com/doc/tutorial/namespaces/
3. Its better to pass the string as const string& url. The & defines this an address reference and the const tells the compiler to not allow the variable to be modified. Its always better to pass by reference or pointer since a 32 bit address is going to be smaller than the actual data structure in most cases.
4. Most languages and scripts have very similar variables, you probably just need to make sure the minimum byte size is the same. I havnt tried to do something like this other than call vb/c++ together
5. You could just declare them at the top of the c file. Its typically better to do it in a header file in case you ever decide to call the function in another c file.
6. Do you still have an issue?
This kind of check is used for headers so variables and functions are not declared more than once. Doesnt make sense for a c file.
#ifndef RUNTIME_H_INCLUDED
#define RUNTIME_H_INCLUDED
...
#endif
 
Last edited by a moderator:
header files.......good
ctime..............for the current time?
namespaces.........good
const string& url..?
32-bit address.....?

#ifndef RUNTIME_H_INCLUDED
#define RUNTIME_H_INCLUDED
...
#endif
was there when i created an header file with codeblocks

a. so you said its better to declare variables in teh header files?
b. and is it also better to add all functions in header also...or have each functions have its won header file?

Code:
i_to_a( int num)
{
string str;
stringstream ss;

ss << num;
ss >> str;

return str;
}

so this should work
Code:
cout << i_to_a(int )<< endl;
 
monstercameron said:
header files.......good
ctime..............for the current time?
namespaces.........good
const string& url..?
32-bit address.....?

Sounds like you need to read up on pointers and references. In short a pointer is variable that contains an address to a object. A reference is way of getting the address.
Code:
   string mystring;
  string* myptr = &mystring;
The benefit is that a 4 byte address (32 bit) is going to be smaller in size then a string to copy to the function.

Just replace all your parameters string mystr with const string& mystr (take out the const if you modify the variable in the function)

monstercameron said:
#ifndef RUNTIME_H_INCLUDED
#define RUNTIME_H_INCLUDED
...
#endif
was there when i created an header file with codeblocks

a. so you said its better to declare variables in teh header files?
b. and is it also better to add all functions in header also...or have each functions have its won header file?

Sorry i didnt realize you defined the functions in the header itself, thought it was a c file. Typically a function is declared in the header and defined in the c file:

Code:
header:
  int myfunc ( int blah );
  
  c file:
  void myfunc ( int blah )
  {
    return blah*2;
  }

But its not critical for the learning stuff your doing. Concentrate on learning actual code, not the format.

monstercameron said:
Code:
   i_to_a( int num)
   {
   string str;
   stringstream ss;
   
   ss << num;
   ss >> str;
   
   return str;
   }

so this should work
Code:
   cout << i_to_a(int )<< endl;

Yep thats how i use it.
 
Last edited by a moderator:
monstercameron said:
moved on from calc to sdl... how do i convert int to string? or is there an easier way to make sprite animations


monstercameron said:
could you elaborate on how to use this codeblock, and whats with the arrays?

Based on your orginial question I figured you wanted to load a number of "numbered" image files for an animation, so this is what this code does.
 
Last edited by a moderator:
Back
Top