Jump to content

Learning C++

dasr1985

Anyone here just absolutely versed in C++?  I'm going through college and with some of the assignments I have to say they are getting pretty difficult.  Outside of the classroom I'm on my own in terms of studying with anyone, or at least having someone to bounce ideas off of.  I wonder if there is anyone here that might not mind me emailing them, or even just conversing through the forum that might be able to answer and "explain" the things I have questions on a bit better than my professor. 

 

Seems like one of those things where, until you're versed in the language it is a bit hard to understand someone else's explanation sometimes of how and why something is used. 

 

Anyhow, comment here or shoot me a PM if you feel inclined.  It is pretty basic stuff but it helps to bounce ideas off of other people for me to learn sometimes given the subject.

 

Thanks in advance

Link to comment
Share on other sites

Link to post
Share on other sites

I'm no pro, but I'd like to think that I can help out in some college courses. The ones I've been taking are ez-pz since I've been working with it on-and-off for a couple of years. 

 

I'll do whatever I can to help, but I know there's a lot more people on this forum more apt than I.

Link to comment
Share on other sites

Link to post
Share on other sites

I appreciate it, I'm literally in the introductory portion of C++ learning basic identifiers, functions, arrays, structs, strings, and building small code for just random stuff to utilize them.  I'll probably post some questions on here tonight when I sit down to work on assignments.  Aside from the labs we do assignments also where the 1st is a compile time bug search, 2nd being run time errors, and the 3rd being a build it yourself.  In no way am I asking anyone to do work for me (just to clarify), I'm just looking to gain a different point of view that might help me understand what the hell I'm using and why, lol.

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't used C++ for a while, but I used to use it at my old job.

If you have basic questions I can probably help, but given that my old manager loved to write C++ code as though it was C code, I can't necessarily help with any really specific C++ library/OOP stuff.

Link to comment
Share on other sites

Link to post
Share on other sites

Feel free I'm always happy to help though I don't always have a lot of free time sadly.

In any event I would suggest that we keep things as public as possible - for everyone's benifit.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah I would just post up your questions or snippets of code you are working on.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
Share on other sites

Link to post
Share on other sites

ok, so the program we are advised to use along the duration of this course is Microsoft Visual Studios 2012.  I updated to 2015 and have noticed no real changes in terms of how it effects my work in the course so far.

 

In our 3rd week now, and I've reached a point in this 'compile time bug' assignment where I'm unsure of what could be causing the next error.

 

Here is the code:

 

 // Week 1 Assignment-1// Description: Compile time errors with loops and branching//---------------------------------- //**begin #include files************#include <iostream> // provides access to cin and cout#include <time.h> // to allow seeding the random number generator using time//--end of #include files-----------//---------------------------------- using namespace std;//---------------------------------- //**begin global constants**********const int arraySize = 5;//--end of global constants---------//---------------------------------- //**begin main program**************int main(){       // seed random number generator       srand(time(NULL));       // create a flag to end the while loop       bool endLoop = false;       // create an int array       int array[arraySize];       // initialize the int array       for (int i = 0; i < arraySize; i++)       {              array[i] = (rand()%50) + 50;       }       int number;       int mysteryNum;       int arrayNum;       while (!endLoop)       {              mysteryNum = rand()%40;              arrayNum = array[rand()%arraySize];              cout << " What number do you need to subtract from " << arrayNum << " to get " << mysteryNum << "? ";              cin >> number;              if (mysteryNum == arrayNum - number)              {              cout << "You got the number!" << endl;              else              {                     cout << "That's not the right number." << endl;                     endLoop = true;              }       }       cin.get();       // Wait for user input to close program when debugging.       cin.get();       return 0;}//--end of main program-------------//---------------------------------- 

 

The last error that I'm getting within the IDE is that the #include libraries for both <iostream> and <time.h> cannot be located.  I verified that they are indeed within the program files of MVS-2015, and I double checked with the instructor that this wasn't an erroneous error and is indeed a symptom of the last error.  There are a total of 5 errors in this source code, 4 of which I have already located and fixed (including in the above paste).  Now I'm just trying to figure out what is causing the error in reading/locating the #include files as I'm fairly certain that is connected to the last error.  I'll keep working on it, but I got stuck over the past 24 hours so I figured I'd throw a hail mary to you guys.  Perhaps if you know the answer, maybe just guide in me in where I'm being a tard about it, lol.

 

Again, thanks guys I really appreciate the help.  Learning this means a lot to me and with the little time I have I'm doing my best to stay ahead of the learning curve.  This is actually the first code problem I couldn't immediately figure out on my own.  I'm betting on it being something pretty simple and dumb.

Link to comment
Share on other sites

Link to post
Share on other sites

 

You may want to edit your post and use code tags, and less colours, so your post will display properly, this is a little sneak-peak how bad it looks now to some of forum users: chrome_2016-01-22_13-36-21.png

Link to comment
Share on other sites

Link to post
Share on other sites

I wave my dumbass flag and say thank you sir!

Text is still black and I use dark theme, but from what I have managed to read you have some installation problem. I would try to reinstall.

Link to comment
Share on other sites

Link to post
Share on other sites

hhmmmm, I'm using the reg white background theme and it looks fine.. I'll prob switch to dark theme to see how I can fix it for the latter.  As far as installation goes, that is what I initially thought was going on, and perhaps it still is.

 

I have a 250 GB SSD that I run my OS on and a few monitoring programs for my system, and I use a 1TB WD Black HDD for everything else. 

 

I initially installed Visual Studios to the 1TB drive, and throughout the course of this assignment and a few from last week I have uninstalled and re-installed MVS probably 4 times now including the last uninstall where I decided to download and install MVS-2015 directly on the SSD just incase installing it on the 1TB HDD was causing the issue. 

 

Now, I have since also double checked with a known good code and it ran just find without any errors or problems with locating the #include files.  When creating new projects it creates a directory folder for the project that would also have the #include libraries as well right?  Perhaps I'll just completely delete the current projects and start over to see if that was the problem when I get home later I guess.

Link to comment
Share on other sites

Link to post
Share on other sites

 

If new projects work then it's fault of configuration of those projects that doesn't Probably wrongly set include directories. If your projects are one file then it should be easy to migrate.

Link to comment
Share on other sites

Link to post
Share on other sites

Just an update, it was a problem with Microsoft Visual Studios 2012.  I ran 2015 and update the library file with the C++ library update and everything is running smoothly for now, fingers crossed.  On the assignment, I also found the last bug.  I'll post an update later tonight in case anybody wants to take a look at the code above and see if they can find it themselves first if they care.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, new prob here:

 

I just finished building this for the 3rd assignment this week, and I have yet to clean it up in terms of organization.  The instructor provided us with the pseudocode to follow which also seemed to included duplicate requests in terms of variables... so those still need to be cleaned up.  I"m gonna take a break and work on physics for a bit before I do that, but I figured I would post what I have here to get some insight.

 

For now it looks like I have errors with part of an 'if' 'else' statement as well as the section where the target is getting moved.  I'll post the errors after the code.

 // Week 3 Assignment-3// Description: Snowball fight - version 1//---------------------------------- //**begin #include files************#include <iostream> // provides access to cin and cout#include <array>#include <vector>#include <time.h>#include "main.h" //--end of #include files-----------//----------------------------------using namespace std;//----------------------------------//**begin global constants********** // define a struct of the targetstruct coords{int x;int y;};struct target{string target;int dist; bool hit;coords position;};target Target;// -- xPos is the x position (the column)int x = (0);// -- yPos is the y position (the row)int y = (0);// -- xHit is x position of the hit(the column)coords xHit;// -- yHit is y position of the hit(the row)coords yHit;// -- distance between target and snowball hitcoords hit_pos;// -- hits is how many times the target has been hitint Tgt_hit_count = (0);// const grid size (i.e. 5x5 grid constant is 5)const int grid_size = 5;// const number of turnsconst int turns = 10; //--end of global constants---------//----------------------------------  //**begin main program**************int main(){ // initializationbool hit = false;int Tgt_hit_count = 0;int dist = 0;coords hit_pos;coords xHit;coords yHit;int throw_snowball(); // seed the random number generatorsrand(time(NULL));// create the target structstruct target{string target;int dist;bool hit;coords position;};target Target;for ((x, y) = 0; (x, y) <= grid_size; (x,y))// set target at random location{Target.position.x = rand() % grid_size;Target.position.y = rand() % grid_size;Target.hit = false;}// set hits to zeroint Tgt_hit_count = 0;// loop for the specified number of turnsfor (int i = 0; i < turns; i++){ //   get x and y position for the snowball from playercout << "select x coordinate for snow ball" << endl;cin.get >> xHit;cout << "select a y coordinate for snow ball" << endl;cin.get >> yHit;}//   compare to the target's positionint throw_snowball(coords position, target Target);{int dist;if (x == Target.position.x);{if (y == Target.position.y);{Target.hit == true;return 0;}else {return abs(y - Target.position.y);}else{int dist = abs(x - Target.position.x);if (int dist < abs(y - Target.position.y)) int dist == abs(y - Target.position.y);}}}//   report hit, or distance of miss (see instructions for details)if (hit) cout << "The Target has been hit!" << endl;else cout << "You achieved " << Tgt_hit_count << " hits out of " << turns << "chances" << endl;cin.get();cin.get();//   target moves (see instruction for details)void moveTarget(target Target);{enum MoveEnum{North, South, East, West, Stop};bool moveNotfound = true;MoveEnum ranNum = MoveEnum(rand() % 5);if (ranNum == Stop) return;while (moveNotfound){switch (ranNum){ case North:if (Target.position.y == 0) break; Target.position.y--;cout << Target << " moving North." << endl;return;  case East:if (Target.position.x == grid_size - 1) break; Target.position.x++;cout << Target << " moving East." << endl;return; case West:if (Target.position.x == 0) break; Target.position.x--;cout << Target << " moving West." << endl;return; case South:if (Target.position.y == grid_size - 1) break;Target.position.y++;cout << Target << "moving South" << endl;return;default:break;}ranNum == MoveEnum(int(ranNum + 1) % 4);}}// end of loop// report score (number of hits vs turns)cout << "Player score is" << Tgt_hit_count << " out of " << turns << " chances" << endl;// Wait for user input to close program when debugging.cin.get();cin.get();return 0;}//--end of main program-------------//---------------------------------- 
Severity Code Description Project File Line Suppression State
 
Error (active) no operator "<<" matches these operands Wk_3_Assignment_3 f:\Devry University Files\Intro to C++ Programming\Assignments\Assignment 3 Week 3\Wk_3_Assignment_3\Wk_3_Assignment_3\main.cpp 144
 
Error (active) expected a statement Wk_3_Assignment_3 f:\Devry University Files\Intro to C++ Programming\Assignments\Assignment 3 Week 3\Wk_3_Assignment_3\Wk_3_Assignment_3\main.cpp 105
 
Error (active) no operator "<<" matches these operands Wk_3_Assignment_3 f:\Devry University Files\Intro to C++ Programming\Assignments\Assignment 3 Week 3\Wk_3_Assignment_3\Wk_3_Assignment_3\main.cpp 138
 
Error (active) no operator "<<" matches these operands Wk_3_Assignment_3 f:\Devry University Files\Intro to C++ Programming\Assignments\Assignment 3 Week 3\Wk_3_Assignment_3\Wk_3_Assignment_3\main.cpp 150
 
Error (active) no operator "<<" matches these operands Wk_3_Assignment_3 f:\Devry University Files\Intro to C++ Programming\Assignments\Assignment 3 Week 3\Wk_3_Assignment_3\Wk_3_Assignment_3\main.cpp 156
 
Link to comment
Share on other sites

Link to post
Share on other sites

 

 

Somthing went wrong with your indentation, but I see you re try to pass Target to cout using << operator.

 

cout is of type ostream and it doesn't know how to handle your Target which is of type struct target.

 

You could overload ostream's << operator to teach it how to handle new type, but easier way for you would be to create function inside struct target that would return string with informations you want to print out. You can use std::strginstream to concancate strings numbers and what not. Example out of my mind, not compiled or anything:

#include <iostream>#include <sstream>using namespace std;struct Test {  int a;  float b;  string c;  string dump(){    stringstream ss;    ss << "Test Struct dump:" << endl << "\ta: " << this->a << endl << "\tb: " << this->b << endl << "\tc: " << this->c;    return ss.str();  }};int main(){  Test testInstance;  testInstance.a = 10;  testInstance.b = 12.3f;  testInstance.c = "What\'s up?";  cout << testInstance.dump() << endl;  return 0;}

As I said, I didn't tried to compile it, but you get idea.  Overloading operator is also not difficult, but functions you may have already know.

This is how it would be done by overloading an "<<" operator:

#include <iostream>#include <sstream>using namespace std;struct Test {  int a;  float b;  string c;};ostream& operator<<(ostream& out, Test& test){  out << "Test Struct dump:" << endl << "\ta: " << test.a << endl << "\tb: " << test.b << endl << "\tc: " << test.c;}int main(){  Test testInstance;  testInstance.a = 10;  testInstance.b = 12.3f;  testInstance.c = "What\'s up?";  cout << testInstance << endl;  return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×