• 0

Newbie C++ Help


Question

Recommended Posts

  • 0

Dont know if its any use, or if its been found before, but IM not looking through 11 pages to see.

<link snipped--copyright infringement>

ITs the whole The

C+ +

Programming

Language

Third Edition

Bjarne Stroustrup

In PDF format XD.

Im using it atm.

Edited by John S.
  • 0

I thought I might add my input into this.

To get a good grounding on programming in general this book is a must read. It's a very tiny handbook as well, created by the founders of the C Programming Language.

Kernighan and Richie - The C Programming Language

It's aptly named as it is a no non-sense approach and tells very well how to make data structures, functions, arrays, and the basic functions of C. Though it took me a while to get my mind around a lot of the stuff written in the book. It all made picture perfect sense. It is a very System Engineering esque approach to programming, and they include the source code for the functions such as fgets, printf, and scanf.

I am learning C++ now, then moving onto C# after (After all, it is an exact copy of Java - Remade to have Microsoft brand on it).Another book was Data Structures and Algorithms with a picture of a waterfall on the front page, but that's for more advanced projects like Hash Tables, and AVL Trees.[That book got me full marks for my First Year University project].

I'm gonna start teaching myself PHP/C/C++ soon and this thread is surely going to help me, but I'm a fair bit older than alot of the people in this thread looking to get into it too (20 as of last month) and I currently really only know HTML well. I was wondering, how long did it take some of you to learn C/C++/PHP to a somewhat decent level from scratch?I'm curious how much time I might need to invest into learning. :)

From the start? Took me a 6 months to a year to get used to C Programming. Far from being a master - Haven't put the effort to become one.I guess you could say I am lost for inspiration to code. Don't know what to code.

  • 0

I'm not sure if this has been addressed, but if it has, feel free to delete this post.

Sometimes a newcomer in C++ can't figure out why after using something like cin >> value, something like cin.get(), which is supposed to wait for data to be entered, doesn't block input (doesn't wait for input) like it is supposed to. The answer is due to the fact that the input buffer that cin uses ends up not accepting the newline character that the Enter key uses when inputting data into variables. For example:

#include &lt;iostream&gt;

using namespace std;

int main () {
	int x = 40;

	cout &lt;&lt; "Enter an integer: ";
	cin &gt;&gt; x;
	cout &lt;&lt; x &lt;&lt; endl &lt;&lt; endl;

	cout &lt;&lt; "Press Enter to exit..." &lt;&lt; endl;
	cout &lt;&lt; cin.bad() &lt;&lt; cin.eof() &lt;&lt; cin.fail() &lt;&lt; endl;
	//cin.clear();
	//cin.sync();
	cin.get();
}

In that simple program, the cin.get() appears to be ignored. The same thing happens with variables of type bool, char, int, float, double, etc. There is a way around this problem, however. Just uncomment the commented lines in the above program and behold the difference! The first commented line clears the error bits, which is necessary because if they are not cleared then the cin.get() will notice the error from the previous read operation and will refuse to try doing an unsafe read. The second syncs the input buffer with the input stream, effectively discarding whatever extra characters are left in the input buffer.

Also note that this happens when doing the same thing with a string variable (an instance of the C++ string class, not a character array). However, it has a much safer solution - getline(cin, string_variable) instead of cin >> string_variable. It works by doing the same thing, except it discards the newline character. You can test this by using cout << string_variable.length() << endl; after using getline() and verifying that the number of characters you typed is the same as the string's length.

It is common for this same thing to happen in C with things like getchar() and scanf. C does not have any way to flush the input buffer, however. To accomplish this same thing, you might use something like while (getchar() != '\n');, or if you have a really crazy C compiler you could use while (getchar() != '\n') { continue; }, though I've never run into this type of thing personally.

I hope this helps people in the future!

  • 0
How different is C# from C++? I'm a beginner to programming, and want to go into C#, but I'm wondering if it'd be best if I learnt C++ first...
They just happen to have the same letter 'C'.

They are 2 completely different languages. C# is basically VB.NET (the functions are common, the same .NET framework) with a Java/C++ syntax and some of its features are taken from both Java and C++. C# for example can inherit from a class, not multiple like in C++ but you can have reference variables like in C++ but with a different syntax though.

  • 0

noob question.... why the below declaration is not good?

#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

class Str {
	public:
		Str(string *s) {
			this-&gt;x=s;
		};
		string getStr() {
			return this-&gt;x;
		}
	private:
		string x;
};

int main() {

	Str d = new Str("BOO");


	cout &lt;&lt; d.getStr() &lt;&lt; endl;
	return 0;
}

and how would a valid code look like :) ty in advance

  • 0
Str(string *s) {
			this-&gt;x=s;
		};

The problem is the fact that you are passing a pointer to a string, but you're trying to assign the pointer to the string. If that sounded confusing, it is -- pointers can be extremely confusing at times. You would need to dereference the pointer.

I recommend the following, though Seismo's solution works just as well:

Str(string const&amp; s) { //pass it by reference
			x=s;
		};

However, if you have your heart set on using a pointer, here is the corrected code:

Str(string *s) {
			x=*s;
		};

  • 0

I have to say, if anyone is really serious about getting into some C or C++ I would seriously consider installing a free operating system such as GNU/Linux or FreeBSD. Once you get really in to it you will want to look at some advanced code for examples... and what better code to look at than the very software you are using! And what better way to learn than to hack it to pieces! Plus GCC is great :D

And for C++, one you have your head around the syntax, pointers, templates etc. I thoroughly recommend this book: The C++ Standard Library by Nicolai M. Josuttis: http://www.amazon.co.uk/C-Standard-Library...553&sr=11-1 Great read, great reference and good way to learn how to use templates and generic programming.

  • 0
ANyone know how to add color to c programming?> I want to change the font color and background color

That's quite an ambiguous question. You can use ASCII or ANSI colour codes in some shells I think... I'm pretty sure it does not work on the standard windows shell any more but it works in bash I think... So already it's becoming platform dependent. Anything else is all platform dependent. If you want to have colour on a text UI then look into ncurses: http://ndk-xx.sourceforge.net/ If you want a GUI then there are many options like GTK.

  • 0

Hi:

I don't know if anyone mentioned this, but I'd be curious if someone could comment on using Codeblocks compared to Bloodshed DevC++ as a noobie IDE. I'm tempted to get a Microsoft Visual C++ but I'm concerned about proprietary lockin in that all I'd ever be able to do is program Windows.

Thanks for any replies.

Jojo

  • 0

can any1 pls help my...why i'm getting this error on compiling

multiple definition of `function(double, double, double, double, double)'

the function is only declared once and there is the code for it, only 1 instance of it is in the whole file...

I'm using CodeBlocks latest nightly build

  • 0

main file

#include "student.cpp"

int main() {

    Student *alin = new Student("Alin", "Bata", "1870513350037", 2008, 6, 3, 5, 8, 4, 9);

    alin-&gt;studentPrint();

    ofstream store ("studenti.txt");
    if (store.is_open()) {
        store &lt;&lt; alin-&gt;getNume() &lt;&lt; alin-&gt;getPrenume() &lt;&lt; alin-&gt;getMediaGenerala() &lt;&lt;
 alin-&gt;getCrediteObtinute() &lt;&lt; endl;
    }
    else cout &lt;&lt; "Unable to open file" &lt;&lt; endl;
    store.close();

    return 0;
}

student.cpp file

#include &lt;iostream&gt;
#include &lt;fstream&gt;

#include "student.h"

using namespace std;

//constructors
Student::Student() {
        this-&gt;nume = "";
        this-&gt;prenume = "";
        this-&gt;cnp = "";
        this-&gt;promotia = 0;
        this-&gt;grupa = 0;
        this-&gt;notaAnalizaMatematica = 0;
        this-&gt;notaProgramareC = 0;
        this-&gt;notaAlgoritmica = 0;
        this-&gt;notaBazeleInformaticii = 0;
        this-&gt;notaEngleza = 0;
        this-&gt;mediaGenerala = 0;
        this-&gt;crediteObtinute = 0;
};

int calculcredite(double notaAnalizaMatematica, double notaProgramareC, double notaAlgoritmica, 
double notaBazeleInformaticii, double notaEngleza);

Student::Student(string nume, string prenume, string cnp, int promotia, int grupa, 
double notaAnalizaMatematica, double notaProgramareC, double notaAlgoritmica, 
double notaBazeleInformaticii, double notaEngleza) {
        this-&gt;nume = nume;
        this-&gt;prenume = prenume;
        this-&gt;cnp = cnp;
        this-&gt;promotia = promotia;
        this-&gt;grupa = grupa;
        this-&gt;notaAnalizaMatematica = notaAnalizaMatematica;
        this-&gt;notaProgramareC = notaProgramareC;
        this-&gt;notaAlgoritmica = notaAlgoritmica;
        this-&gt;notaBazeleInformaticii = notaBazeleInformaticii;
        this-&gt;notaEngleza = notaEngleza;
        this-&gt;mediaGenerala = ((notaAnalizaMatematica + notaProgramareC 
+ notaAlgoritmica + notaBazeleInformaticii + notaEngleza) / 5);
        this-&gt;crediteObtinute = calculcredite(notaAnalizaMatematica, 
notaProgramareC, notaAlgoritmica, notaBazeleInformaticii, notaEngleza);
};

int calculcredite(double notaAnalizaMatematica, double notaProgramareC, 
double notaAlgoritmica, double notaBazeleInformaticii, double notaEngleza) {
    int temp = 0;
    if(notaAnalizaMatematica &gt;= 5) {
        temp = temp + 6;
    }
    if(notaProgramareC &gt;= 5) {
        temp = temp + 6;
    }
    if(notaAlgoritmica &gt;= 5) {
        temp = temp + 6;
    }
    if(notaBazeleInformaticii &gt;= 5) {
        temp = temp + 6;
    }
    if(notaEngleza &gt;= 5) {
        temp = temp + 6;
    }
    return temp;
};

//deconstructors

//get functions
double Student::getMediaGenerala(void) {
    return this-&gt;mediaGenerala;
};
int Student::getCrediteObtinute(void) {
    return this-&gt;crediteObtinute;
};
string Student::getNume(void) {
    return this-&gt;nume;
};
string Student::getPrenume(void) {
    return this-&gt;prenume;
};

//print class
void Student::studentPrint(void) {
    cout &lt;&lt; this-&gt;nume &lt;&lt; this-&gt;prenume &lt;&lt; this-&gt;cnp &lt;&lt; this-&gt;promotia &lt;&lt; this-&gt;grupa &lt;&lt; 
this-&gt;notaAnalizaMatematica &lt;&lt; this-&gt;notaProgramareC &lt;&lt; this-&gt;notaProgramareC &lt;&lt;
 this-&gt;notaAlgoritmica &lt;&lt; this-&gt;notaBazeleInformaticii &lt;&lt; this-&gt;notaEngleza &lt;&lt; 
this-&gt;mediaGenerala &lt;&lt; this-&gt;crediteObtinute &lt;&lt; endl;
};

student.h file //here is the class declared

#include &lt;string&gt;

using namespace std;

class Student {
    public:
        Student();
        Student(string nume, string prenume, string cnp, int promotia, int grupa, double notaAnalizaMatematica, 
double notaProgramareC, double notaAlgoritmica, double notaBazeleInformaticii, double notaEngleza);
        ~Student();
//get functions
        string getNume(void);
        string getPrenume(void);
        double getMediaGenerala(void);
        int getCrediteObtinute(void);
//print function
        void studentPrint(void);
    private:
        string nume;
        string prenume;
        string cnp;
        int promotia;
        int grupa;
        double notaAnalizaMatematica;
        double notaProgramareC;
        double notaAlgoritmica;
        double notaBazeleInformaticii;
        double notaEngleza;
        double mediaGenerala;
        int crediteObtinute;
};

ty

  • 0
At first, I didn't really though many people would reply, Thanks. First, I think I am going to get started with C then move on to C++ and maybe create my first program(thinking too far ahead). Oh yeah, I am also 13 and I figure this is the best time to learn since I am yuong and I will get materials better. Again, Thanks for everything.

Ok shouldn't you try learning Visual Basic first? I tryed learning other languages first like WPF and stuff like that, it never worked out for me because Visual Basic is the beginning for programming. I never understood how programming worked, until I learned a little Visual Basic, which would help you learn the next programming language. Well I know this post is old, but it also is still valid for asking this question.

  • 0

I know I'm a little (very) late in this thread, but...

C++ is old! move to Java! it's much much better!

And better yet, focus on internet technologies, PHP or JSP or JSF and AJAX/CSS/XHTML

It's more fun than the old boring languages!

w3schools.com one of the best sources for most of them!

Edited by nonick
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Microsoft: Windows 11 KB5094126, KB5093998 finally stops trusting a critical system threat by Sayan Sen This week Microsoft released the Patch Tuesday updates for June 2026 with KB5094126 on Windows 11 25H2, 24H2, and KB5093998 on Windows 11 23H2. On Windows 10 22H2 it's under KB5094127. Alongside the announced release notes for the new builds, Microsoft has revealed another change that is coming to Windows with these new releases. It has been confirmed that custom folders are getting a significant change with the June 2026 updates as such folders or folder names defined by desktop.ini will no longer appear after this update is successfully installed. While you may inititally think this is a bug with the new release, Microsoft has stated that this is in fact "expected behaviour" in its new support article regarding this which Neowin spotted today while browsing. Essentially it's a security hardening measure such that custom folder presentations are treated as potentially unsafe whenever Windows is not sure about their origin and whether that desktop.ini folder can be trusted or not. Here is list of such untrusted files and folders: Files downloaded from the internet that carry Mark-of-the-Web (MOTW). Files copied from certain remote locations, such as some WebDAV or HTTP-based locations. Files on network paths that are not classified as intranet or trusted by zone policy. For those who may not be familiar, Desktop.ini is a special configuration file used by Windows to customize the appearance and behavior of individual folders. Basically Windows can read specific instructions stored in Desktop.ini instead of displaying every folder with the same default settings. This can be used to apply custom icons, thumbnail images, localized folder names, and such informational tooltips (infotip). The file can also influence certain folder-specific behaviors and properties. It is typically stored as a hidden system file within a folder that has been designated to support Desktop.ini customization. However, because Windows Shell automatically reads and applies these attributes whenever a customized folder is opened, they have historically (since the Windows XP days) presented an attack surface as a result of an unchecked buffer in the Shell component responsible for extracting custom attributes from Desktop.ini files. As such an attacker could create a specially crafted Desktop.ini containing a malicious or corrupted attributes and place it on a network share. So if a user were to browse that folder, Windows would automatically process the file, potentially triggering a buffer overflow. This could allow arbitrary code to run with the same permissions as the logged-in user. Hence a seemingly harmless folder could become a security risk when their contents are not properly validated. For admins and users alike looking to manage this behaviour, Microsoft has shared a few ways. One of them is to assign a trusted mark on the folder in case you are sure of its source. Secondly a policy can be used to revert back to the previous state. Finally, the MOTW can be removed too to indicate to Windows that this is a safe file. The company explains: Option 1: Add the source to Trusted Sites (Recommended) If the affected content is stored on a known internal or managed source, add that source to the Trusted Sites list. Once the source is treated as trusted, Windows processes desktop.ini from that source normally. This keeps the protection in place for other locations and is the lower-risk option. Option 2: Use policy to restore previous behavior Organizations that need broader compatibility can enable the policy Allow the use of remote paths in file shortcut icons.Enabling this policy restores the pre-June 2026 behavior for affected remote or untrusted scenarios. Option 3: Check for and remove the Mark of the Web (MotW) If the desktop.ini file has a Mark of the Web (MotW), Windows may treat it as coming from an untrusted source and block customization. Verify whether MotW is present and, if appropriate, remove it from the desktop.ini file. This can restore expected behavior, but should only be done for trusted content, as it removes the associated security protection. To remove the MotW tag, open PowerShell and run one of the following commands: For a single desktop.ini file: Unblock-File "C:\Your\Folder\Path\desktop.ini" For all desktop.ini files in a folder: Get-ChildItem "C:\Your\Folder\Path" -Recurse -Filter desktop.ini -Force | Unblock-File Microsoft has warned though against using a broad opt-out using the provided policy as it reduces protection against potentially malicious remote folder-customization content. As such the tech giant recommends trusting only controlled internal sources and keeping trust settings as strict as possible. You can check out the official support article here on Microsoft's website.
    • LAV Filters 0.82.0 by Razvan Serea LAVFSplitter is a multi-format media splitter that uses libavformat (the demuxing library from ffmpeg) to demux all sorts of media files. LAV Splitter is a Souce Filter/Splitter required to demux the files into their separate elementary streams. LAV Audio and Video Decoder are powerful decoders with a focus on quality and performance, without any compromises. Supported Formats: MKV/WebM, AVI, MP4/MOV, MPEG-TS/PS (including basic EVO support), FLV, OGG, and many more that are supported by ffmpeg! LAV Filters are based on ffmpeg and libbluray and is aimed to offer a all-around solution to perfect playback of file-based Media as well as Blu-rays. LAV Filters 0.82.0 changelog: LAV Splitter NEW: Support for demuxing Dolby Vision Enhancement Layer streams NEW: Support for Animated WebP images Changed: When demuxing Blu-ray discs, Dolby Vision metadata is available on the primary video stream LAV Video NEW: Support for Animated WebP images Changed: Hardware decoding support for DVDs has been removed Download: LAV Filters 0.82.0 | 15.5 MB (Open Source) View: LAV Filters Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • For some reason when EU forced Microsoft to allow users to change the default browser and search provider in Windows (also no ads for Office and the likes) - it was good. But when it comes to Apple - then it's bad. BTW, Apple would have gone out of business if Microsoft wasn't pressed by US government several decades ago. 😉
  • Recent Achievements

    • One Month Later
      Sopa flores earned a badge
      One Month Later
    • First Post
      StaticMatrix earned a badge
      First Post
    • Week One Done
      StaticMatrix earned a badge
      Week One Done
    • Rookie
      lamborghiniv10 went up a rank
      Rookie
    • One Month Later
      pinnclepd earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      PsYcHoKiLLa
      207
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      88
    5. 5
      ATLien_0
      79
  • Tell a friend

    Love Neowin? Tell a friend!