C++ Programming help

Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum

Help Support Homebrew Talk - Beer, Wine, Mead, & Cider Brewing Discussion Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

SporkD2

Well-Known Member
Joined
Jun 6, 2008
Messages
736
Reaction score
1
Location
San Antonio
Anyone on this board a good c++ programmer? I am currently a computer sci major and programming is hard for me to understand. I would love to have someone who I could email questions and maybe have a few chats about things that are confusing me.

Thanks

Please contact me via aim, yahoo messenger or [email protected]
 
For your sake, I hope that your major is focused in areas other than programming (hardware, architecture, etc). My degree is in computer science, and we really focused on code/software development. C++ is a pretty simple language. What's confusing?
 
You can pick my brain too.
One of the best books I have is Jamsas C/C++ programming bible. Highly recomend it.
 
Well my emphasis is computer networking, not programming. I have already taken the intro course and am now into the 2nd out of 3 c++ classes. Just looking for someone to answer some simple questions about the programs I have to write for when I get stuck.

For example:

I have to take a user inputted string, such as AAA765, make sure that the first three characters are uppercase (toupper) and then do a binary search from an array I have created and show a fine amount. I can do this if the user enters their license plate number as an uppercase, but if they enter lowercase, it comes back as not found.
 
Well to address the lower case issue.
I am using lplate as the input string
You can do something like
Code:
char *str=&lpate;
for (int i=0 : i<strlen(str) : i++)
{
  if (islower(*(str+i)))
    toupper(*(str+i));
}
Now this is C, not C++. If you're expected to use some Class and it's methods to do this we need to know what object you are using. Or are you building the clasS?

Edit: fixed syntax
 
Maybe this will help:
Code:
#include <iostream>
#include <ctype.h>
#include <string>

using namespace std;

void makeupper (string& s)
{
  int i = 0;
  while (s[i])
  {
    s[i] = (toupper(s[i]));
    i++;
  }
}

int main ()
{
  int i = 0;
  string str = "It's 5 o'clock somewhere!\n";
  makeupper(str);
  cout << str;
  return 0;
}

EDIT: Damn, Virtuous beat me to it. I like mine better. :drunk:
 
Maybe this will help:
Code:
#include <iostream>
#include <string>

using namespace std;

void makeupper (string& s)
{
  int i = 0;
  while (s[i])
  {
    s[i] = (toupper(s[i]));
    i++;
  }
}

int main ()
{
  int i = 0;
  string str = "It's 5 o'clock somewhere!\n";
  makeupper(str);
  cout << str;
  return 0;
}

EDIT: Damn, Virtuous beat me to it. I like mine better. :drunk:


You need ctype.h in there too. Cant make it too easy for him though ;) That and I dont remember if toupper() checks if a char is a lower case alpha. Probably does
 
Why not derive a custom string class from std::string and implement makeupper() as a member function ? You are programming in C++ and not C you know...
 
toupper() is pretty smart. It will return uppercase, numbers, and special characters appropriately (i.e., it doesn't get weird if you send it something other than a lowercase letter).
 
Why not derive a custom string class from std::string and implement makeupper() as a member function ? You are programming in C++ and not C you know...

I had taken C++ classes only to spend 80% of the semester doing C and then they finally moved into OOP. Not sure where he is at, he certainly has enough information to do that now ;)
 
Mostly because I couldn't remember how. I've been messing with Java for too long, and C++ is just different enough to throw me for a loop.

No wonder you didnt like my pointers, Always hated that abou Java. Damn it I want to manipulate mamory, err memory directly!
 
Ya, some of that is a few steps ahead of what I am doing. This week is mostly concentrating on making arrays and sorting them.
 
You need ctype.h in there too. Cant make it too easy for him though ;) That and I dont remember if toupper() checks if a char is a lower case alpha. Probably does

That code did it, I am not sure why my version wasnt working, but possibly cause I wasnt using a loop, i was just trying to set 3 different char and do it that way.

Thanks a ton, still though, if anyone has aim or yahoo messenger I seem to also get stuck on these small problems. Would be great to have someone within a quick message.

Thanks again, im sure more to come
 
Spork, here's a brute force example for the first three characters:

Code:
#include <iostream>
#include <ctype.h>
#include <string>

using namespace std;

int main ()
{
  string str = "abc123\n";
  str[0] = toupper(str[0]);
  str[1] = toupper(str[1]);
  str[2] = toupper(str[2]);
  cout << str;
  return 0;
}
 
Spork, here's a brute force example for the first three characters:

Code:
#include <iostream>
#include <ctype.h>
#include <string>

using namespace std;

int main ()
{
  string str = "abc123\n";
  str[0] = toupper(str[0]);
  str[1] = toupper(str[1]);
  str[2] = toupper(str[2]);
  cout << str;
  return 0;
}

I tried to do this same thing, just to debug and to make sure this would work, I did just the first char and tried to toupper it with no result... Weird
 
I still have to add something to where if the user just inputs an E that the program will exit, but that shouldnt be hard
 
yuck

you're supposed to be programming in C++ right ?

string plates[]; // GOD NO - PLEASE DON'T

std::vector<std::string> plates; // Lovely Goodness

if you do it this way you'll find that that all that sort array stuff becomes much simpler

eg:
std::sort(plates.begin(), plates.end());

you'd get the binary_search for free as well instead of writing your own - honestly the world doesn't need any more binary search functions, yours or your teacher's won't be better than the one in the STL anyway
 
I'm guessing the point of the assignment was to write a binary search function, and it appears that using arrays was a part of it. There are certainly better ways to achieve the end state, but based on the code he's written so far, I don't think they've delved too deeply into OO yet.

Gotta walk before you can run, and these sorting/searching exercises are pretty common ways to intro programming logic.
 
That's something I discovered while taking Java and C++; the books often have you do something the hard/long and drawn-out way as an "exercise" and the easy way is buried in later chapters.

Sorry I can't help, but I promptly forgot any coding I had to learn. Why Cal State Frenso wants mechanical engineers to know C++ is beyond me.
 
yuck

you're supposed to be programming in C++ right ?

string plates[]; // GOD NO - PLEASE DON'T

std::vector<std::string> plates; // Lovely Goodness

if you do it this way you'll find that that all that sort array stuff becomes much simpler

eg:
std::sort(plates.begin(), plates.end());

you'd get the binary_search for free as well instead of writing your own - honestly the world doesn't need any more binary search functions, yours or your teacher's won't be better than the one in the STL anyway

That seems much easier, but I have a feeling if I included that I would have had a bad score.
 
For your sake, I hope that your major is focused in areas other than programming (hardware, architecture, etc). My degree is in computer science, and we really focused on code/software development. C++ is a pretty simple language. What's confusing?

I use C++ pretty regularly in my job. Even though I feel I can easily understand C++ and make use of most of the features I do not think it is a simple language. Object oriented programming alone is a difficult concept for most people to grasp and the manipulation of pointers and references can get very tricky to manage if you do not understand how they are handled by the compiler.

If you continue to post questions here I will try to answer but C++ is not the primary language I use so I often have to look up what library methods, classes and templates are available.

If you can truly understand C++ and OOP then Java, Python and the myriad other OOP languages become pretty simple to pick up.

Craig
 
Hey, I passed an argument by reference...
I never been keen on pointers myself. The worst is a pointer to a pointer to a pointer. :mad:

When I was doing my Computer Engineering undergraduate at Michigan State 95-99, they somewhat discouraged pointer use. I think for good reason too. You can get in a lot of trouble if you are not too careful. Pointers have their use, don't get me wrong, but it is not always clear what the code is attempting to do. I like my code clear and concise.
 
I use C++ pretty regularly in my job. Even though I feel I can easily understand C++ and make use of most of the features I do not think it is a simple language. Object oriented programming alone is a difficult concept for most people to grasp and the manipulation of pointers and references can get very tricky to manage if you do not understand how they are handled by the compiler.

If you continue to post questions here I will try to answer but C++ is not the primary language I use so I often have to look up what library methods, classes and templates are available.

If you can truly understand C++ and OOP then Java, Python and the myriad other OOP languages become pretty simple to pick up.

Craig
Java is a great OOP language, because you are forced to use classes. In comparison C++ can be sloppy. I'm surprised that the OP's Computer Science Department hasn't switched over to Java. I know Rutgers has.

Anyways, I think it is best to try to figure out the code yourself instead of asking us. It's the best way to learn how to do something. I've become a pretty decent programmer over the years, but I sure as heck did not start out that way. To be honest I really sucked at first. :)
 
I never been keen on pointers myself. The worst is a pointer to a pointer to a pointer. :mad:

When I was doing my Computer Engineering undergraduate at Michigan State 95-99, they somewhat discouraged pointer use. I think for good reason too. You can get in a lot of trouble if you are not too careful. Pointers have their use, don't get me wrong, but it is not always clear what the code is attempting to do. I like my code clear and concise.

You bit twiddling WUSSIES!
I love pointers, although that was not always the case. When I fully understand them it was like an epiphany. My favorite where function pointers.

but yeah things like ********ptr gets aggervating :)
 
I find most students have a hard time with pointers because its taught terribly by most professors. Most really need a good clear illustrative example of how they work to fully grasp it.
 
that was string& str I just passed by reference, didnt know I was using pointers (whatever that is)
 
I think pointers can be a difficult concept to grasp but one absolutely necessary to being a good programmer. I nearly any language you work with you will be using pointers either explicitly (C) or indirectly (Java). How they are work is something that is important to understand to write efficient code.

In C/C++ arrays (x[]) , pointers (*ptr) and references (&ref) are all really pointers underneath.


Now I'm going off and confusing the new programmer :confused:

Sorry I'll try to stay on topic from now on. :D

Craig
 
I find most students have a hard time with pointers because its taught terribly by most professors. Most really need a good clear illustrative example of how they work to fully grasp it.

Couldn't agree more. I had no idea what a pointer was until I got a job as a computer programmer and had to teach myself, or be taught by co-workers.
 
AHH! I just finished two full weeks coding a .Net application at work only to see C++. I'm going to fridge, grabbing a beer, and getting in the pool.
 
I think pointers can be a difficult concept to grasp but one absolutely necessary to being a good programmer. I nearly any language you work with you will be using pointers either explicitly (C) or indirectly (Java). How they are work is something that is important to understand to write efficient code.

In C/C++ arrays (x[]) , pointers (*ptr) and references (&ref) are all really pointers underneath.


Now I'm going off and confusing the new programmer :confused:

Sorry I'll try to stay on topic from now on. :D

Craig

That is what I was talking about really. Passing function arguments by reference is a bit neater. Yes it is still a pointer, but the code is a little more readable.
 
C and C++ are the devil :D. The problem with these, especially C++ is that the code can easily be obscured. You should see some of the code generated stuff from Simulink or Rhapsody.
 
Back
Top