C/C++ Guide to C++ Programming For Beginners

AI Thread Summary
The discussion focuses on a beginner's guide to C++ programming, emphasizing the importance of selecting a suitable integrated development environment (IDE), such as Bloodshed’s Dev-C++, for compiling and debugging code. Participants express appreciation for the tutorial's structure and suggest including deeper explanations of key concepts like conditional statements, iterative statements, and pointers. There is a request for clearer code examples to enhance understanding, particularly regarding function definitions and the use of header files. The conversation also touches on the potential for future tutorials on GUI toolkits and advanced topics. Overall, the thread serves as a collaborative platform for sharing insights and improving the learning experience in C++.
  • #151
DarkShadows5 said:
your tutorials are great so far but i was wondering why every time i run one of the assignments it only shows the window for a split second?
I typed everything in exactly and it does this except however the "hello world" example does not disappear as soon as it appears.
it's quite annoying to have to "try" to hit prtsc key when it pops up to see what it says... lol


haha NVM I figured it out instead of using

return 0;

use

system("PAUSE");
return EXIT_SUCCESS;

and it will pause it so you can view the information.
 
Technology news on Phys.org
  • #152
what is function in c++ ? define and elaborate it completely?
 
  • #153
I didn't see this mentioned in the tutorial, but what is the preprocessor and its function?
 
  • #154
vanmaiden said:
I didn't see this mentioned in the tutorial, but what is the preprocessor and its function?

Sounds like homework.

Try google. :) http://en.wikipedia.org/wiki/C_preprocessor [Note: The C preprocessor is the C++ preprocessor.]
 
  • #155
TylerH said:
Sounds like homework.

Try google. :) http://en.wikipedia.org/wiki/C_preprocessor [Note: The C preprocessor is the C++ preprocessor.]

I wasn't quite sure if it was or not, but thank you for the link! :smile:
 
  • #156
The link is really very useful! thanks for it!Have saved my brain from melting)))
 
  • #157
DarkShadows5 said:
haha NVM I figured it out instead of using

return 0;

use

system("PAUSE");
return EXIT_SUCCESS;

and it will pause it so you can view the information.

Ok I understand that it should work however when I do it I only get a cmd screen displaying "press any key to continue.." and not even a split second of screen display but without it i do
 
  • #158
how to handle button events in turbo c++...
i am developing project for "Mines Sweeper" game using turbo c++
 
  • #159
I read your tutorial, and liked it!

Now I am writing a text-based adventure game. I was wondering, though.

If someone types something like 'help' or 'exit game' or something, I want to always give the same reply to that. How do I do that without typing something like

getline(cin,a);
if(a=='help'){
cout<<'this sentense replies to help.\n'
}

in EVERY function i?
 
  • #160
In general, any time you find yourself wanting to do the same thing over and over again in different parts of a program, you should consider writing a function to do it for you.
 
  • #161
Thank you jtbell, but I'm not sure that is what I meant. Even if I write a function to do something like this, I still have to call the function again and again. I was wondering if there is a way to make some global statement like:

if anywhere at any point, the user input to this program is the following string: 'xxxxx', then make the following reply: 'yyyyy'.
 
  • #162
Well, you should try to work with classes. As there is no way the program does it automatically for you.

You should learn some OOP, and it will make your life easier when you work with C++.

That's how program's work. They call functions. For example, each time you post something on the forum, the post function of the forum is invoked, so there is no way in escaping from it. The only thing you can do is make your code maintainable and readable.

Of course you could write it in a modular way, using functions only, but without functions it's impossible. Cheers
 
  • #163
log(15)/log(2)=?

Hi, I just started with you C++ tutorial.

In lesson #3 you do write: log(15)/log(2)=4.9

Is that correct? In my calculator is says: 3,9.

Rounding up gives 4. And four bytes are just what's needed to store 15 decimal, or
FFFF hexadecimal, or 1111 binary if I'm on the right track.
 
  • #164


Janno said:
Hi, I just started with you C++ tutorial.

In lesson #3 you do write: log(15)/log(2)=4.9

Is that correct? In my calculator is says: 3,9.
No, the calculation in the tutorial is incorrect. The value you have is correct.
Janno said:
Rounding up gives 4. And four bytes are just what's needed to store 15 decimal, or
FFFF hexadecimal, or 1111 binary if I'm on the right track.

Yes and no. It takes only 4 bits (not bytes) to store the value for 15. With four bytes you can store an unsigned integer in the range 0 through 4,294,967,295.

I'll go back and fix the tutorial.
 
Last edited:
  • Like
Likes mafagafo
  • #165
I must confess that my biggest obstacle to use C or C++ where the strange stories about how to read in or write files. In fortran, you have a read or write statement and it is part of the language. In C/C++ this seems to be different; you need all kinds of libraries, and it seems to be arbitrarily complicated to correctly handle the end of a line :-)

It would really be grateful for some lucid explanation on how to read and write in these languages.
 
  • #166
DrDu said:
In fortran, you have a read or write statement and it is part of the language. In C/C++ this seems to be different; you need all kinds of libraries, and it seems to be arbitrarily complicated to correctly handle the end of a line :-)

It would really be grateful for some lucid explanation on how to read and write in these languages.

It indeed comes with libraries of a kind, but you don't have to link them manually, the compiler suite does that for you by default.
What you do have to do manually is including the appropriate headers:

E.g. for C :
Code:
#include <stdio.h>

int main() {
    
    FILE* fp = fopen("myfile.txt","w");

    if (! fp)  {
        /* handle errors */
    }  else {
        fprintf(fp,"Hello World!\n"); /* prints one line to the file */
        fclose(fp);             
    }
}
E.g. for C++:
Code:
#include <iostream>  // for std::cerr etc
#include <fstream>   // for file handling

const char* FILENAME = "myfile.txt";

int main() {
    std::ofstream ofs(FILENAME);
    if (! ofs)  {
        std::cerr <<  "Error opening " << FILENAME << std::endl;  
    }  else {
        ofs << "Hello World!" << std::endl;
        ofs.close(); // not mandatory; the "ofs" d'tor would do that anyway
    }
}

There's of course MUCH MORE to be said about this, but you find this all this in good introductory textbooks or on the WWW.

If you are using Linux, also consider consulting the man pages, e.g.
Code:
man fopen
takes you right to the topic.

Cheers, Solkar
 
  • #167
Hi,, I like this post and all comments,, I need classes in programming or just C++ for beginners,, best if distance-learning, well.. I've never done any of CS, but can you manage to self-study the course, or an instructor always better?Thanks
 
  • #168
Hello,
I started learning C++ a few weeks ago and I'm learning how to solve problems using C++,I was trying to solve this problem http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2160
using this code
Code:
#include <iostream>
using namespace std;
int main()
{
	int D1, M1, Y1, D2, M2, Y2, T;
	cin >> T;
	cout << endl;
	for (int i = 1; i <= T; i++)
	{
		cin >> D1 >> M1 >> Y1 >> D2 >> M2 >> Y2;
		cout << endl;
		int Days = D1 - D2;
		int Months = M1 - M2;
		int Years = Y1 - Y2;
		if ((Months < 0) || (Days < 0 && Months == 0))
		{
			Years = Years--;
			if (Years > 130)
			{
				cout << "Case " << "#" << i << ": " << "Check birth date" << endl;
				cout << endl;
			}
			else if (Years < 0)
			{
				cout << "Case " << "#" << i << ": " << "Invalid birth date" << endl;
				cout << endl;
			}
			else
			{
				cout << "Case " << "#" << i << ": " << Years << endl;
				cout << endl;
			}
		}
		else if (Years > 130)
		{
			cout << "Case " << "#" << i << ": " << "Check birth date" << endl;
			cout << endl;
		}
		else if (Years < 0)
		{
			cout << "Case " << "#" << i << ": " << "Invalid birth date" << endl;
			cout << endl;
		}
		else
		{
			cout << "Case " << "#" << i << ": " << Years << endl;
			cout << endl;
		}
	}
	return 0;
}
But when I submit it on the site it says wrong answer,so is there something wrong with my code?
 
  • #169
I have bought and almost finished reading C++ Programming in easy steps by Mike Mcgrath. It is very good.
The next book I will be reading will be Beginning Visual C++ 2012 by Ivor Horton.
 
  • #170
Oh yea, this Dev-C++ 5.6.1 is a very good tool for learning C++, though it still crashes on my Windows 7.
 
Back
Top