Its a bit specific, but in your case, when 9.4 goes to 9 and 73oiasfk goes to 73, the remaining characters should be left in the stream. So for instance, if you look inside the myStream varaible, you should be able to recognize that there are still characters left inside of it, and error out accordingly. Something like this
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
int input_var = 0;
string num ="";
do {
cout << "Enter an integer (-1 = quit): ";
getline(cin, num);
stringstream myStream(num);
myStream >> input_var;
if (myStream.str().size() > 0) {
cout << "You entered a non-integer. Exiting..." << endl;
break;
}
if (input_var != -1) {
cout << "You entered " << input_var << endl;
}
} while (input_var != -1);
cout << "All done." << endl;
system("PAUSE");
}
Unfortunately this is completely a guess as Iāve not really used stringstream before, but in theory this should work. You can also just loop manually through the string, checking each digit. Outside of a ā-ā in the first position, anything other than 0-9 is an invalid integer and you can act accordingly. Something like this:
bool isInt(string text){
for(int i = 0; i < text.size(); i++)
{
if(text* < '0' || text* > '9') {
//non numeric. Test for negative sign in first position
if(i != 0 && text* != '-' )
return false;
}
return true;
}
Otherwise you can use strtol() like people have been doing for the last million years. Which feels very C instead of C++ but hey, itās valid.
string input; // get this from wherever
char *inputPtr = input.c_str();
char *lastChar;
long int intValue = strtol(inputPtr, &lastChar, 0);
// There are other ways to check this, but this should be fine
if (*lastChar != '\0')
{
cout << "Your entry \"" << input << "\" is not a valid integer
";
}
else
{
cout << "You entered the number " << intValue << "
";
}
(Using stoi() is similar but without pointers being involved.)
Thanks for all of your replies. After some reading, I actually figured out a few different ways of doing it. It just seemed like a much more involved process than I thought itād be.
Sorry to turn this into ājob advice for axemanā yet again, but Iām in a bit of a fork situation here:
After a year of searching for a job, I got a part-time web developer position at a small 7-person company in my city. I like the company and my co-workers, but I may lose the job in less than a year. There are two possible reasons why:
1.) I was hired to help with specific projects. Once those projects are done with, I might be gone. While the boss has said he would try to bring in more projects to keep me on, neither of us know what the future holds. This company doesnāt seem to have much work aside from a few key clients. I know for sure that Iāll be on my particular projects until at least the end of this year, though.
2.) I may jump ship on my own because the pay overall is low. I make $19 an hour at part-time hours. So, thatās about 30K a year pre-tax. The best āraiseā Iām looking at in this place in the near future is to go full-time. Thatāll bump me to around 38K. While thatās better than the 0 I was getting for this past year, I know this salary is low and canāt support me long term if something major were to happen to me.
The reason I bring this up is my job history. It took 7 months after graduating college 3 years ago to find a job. While I was at that job for a year and a half, I was laid off, and spent another year finding a job. At the moment, those unemployment gaps combined are just as big as my time actually working. If I were to lose this current job in under a year through either 1 or 2 above, I feel it will look like Iām unstable on my resume. How do I combat this (assuming Iām right)?
30k for a web developer is low anywhere I think, but it really depends what web developer means. Creating static html, something middle ground like php, or more web server java/webservice stuff as APIs for apps
I live in Flint, MI. Aināt shit around here unless youāre already experienced. Part of the reason it took so long to find a job is that I lacked the money to move somewhere else and kept my search to places within an hour of Flint. I know the money is peanuts, but I really needed a job at the time and feel the experience could be good for me down the road.
Youāre right Pimp; the projects Iām on right now arenāt intensive. Just some ASP.Net/Javascript stuff. But I hope to learn from them and branch off into more complex stuff. Iām really just starting out as a web developer. My main strengths (which still werenāt super strong) before this job were in C# (desktop). However, I knew enough Javascript and HTML stuff (through self-study) that the boss felt I was able to tackle some things when I applied.
Thereās still that resume thing though. How do I handle having huge gaps on my resume AND my most recent job lasting less than a year (assuming that happens)? Maybe it was in my head, but I think being unemployed for more than 6 months was sending a lot of red flags through employersā heads. I was applying a lot, but didnāt get much of any interviews. I donāt want to be in that purgatory again, but donāt know how to avoid it if this job falls through. Iāll still be too broke to move without risking my nest egg. The thing is, I donāt want to start applying for other work and maybe get an offer before these projects are done. But after theyāre done, who knows? If I smell a canning about to happen, Iāll try to jump ship before Iām laid off again. But how will I know it before it happens?
A lot of web developer stuff can be done remotely, I have no idea what the local job market is like but I canāt imagine its that good for tech in michigan. In my area, cali, I am probably contacted by headhunters once a week. Maybe you can find something remote?
See, now i may sound like a jerk for saying this, but certain languages are easier to write and understand while producing the same outcome. I mean, why use C++ if you can use C#? Why use C# if you can use VB. I like VB. You donāt have to worry about silly ācurly bracesā and stuff like that. I donāt understand why people say ālazy programmersā or āyou are not a programmerā if you donāt use language x or language y. For example, in my experience, using stored procedure and writing the bulk of your code with Transact-SQL is more effecient than using views or using other languages to write the bulk within an MS SQL databaseā¦
This is utterly random, but I thought of something: why donāt we talk about the absolute lowest or stupidest job offers we got? Just for fun. While I was running through my head about my current job, I reminded myself about an abysmal offer I had February of this year (6 months after my layoff).
It was special in all that preceded it: I applied for a job at a insurance software company, and after a phone interview the head IT guy decided I didnāt have what they were looking for. However, I talked about getting stuff up on GitHub to make myself more marketable, and he suggested to me a project I could do to put up: Invaders (the video game). There was a lab in the Head First seriesā C# book for building the game. They donāt do everything for you; you have to piece it together from previous book knowledge and hints here and there. If I was able to get that (and more things of that depth) up there, that might be at least something to talk about in interviews. I DLed the book and went through it up until I had that game done. That game is now one of two big projects on my GitHub (I know; I need more).
I called the guy back saying I completed it. I was hoping for just critiques, but he set up a phone/GoToMeeting interview. They saw the game from my screen, and decided to bring me in for a real interview. At that interview, the head IT guy and a potential co-worker of mine had a long conversation about how Iād have to move closer if I didnāt want the commute to wear me down in a few months. I forgot to mention: the company was in Troy, MI, which was an hour away from Flint in distance and apparently light years away in standard of living. This will be important very soon. After the convo, they tested me by having me change some things about the game (forgot what they were) to gauge how I designed things. I passed the test, because they started talking about how things are for the company in learning curve. It would take me 6 to 9 months to get up to speed with their systems. They had huge programs. After that convo, they said theyād be happy to put me onā¦ for $22,000 to start.
ā¦
ā¦
We had just spoken for about 15 minutes about how Iād have to move to Troy, which was more expensive than Flint. Until I found an apartment, Iād have to bear the commute, which would quadruple my gas costs. Thereās a 6 month learning curve to the companyās stuff, meaning I probably wonāt get any type of raise for at least that time. But they wanted to start me out at 22K. The head guy suggested I take a week to think on it, but I think we both knew I wasnāt taking it by my facial reaction. I was like Thor in the Ultron trailer when Cap got the hammer to move just a bit. Maybe not that obvious, but it was clear any happiness I had up until that point was dead. We said peaceful goodbyes and I let them know Iād be passing a week later.
Iām happy I did the C# project and the GitHub stuff; Iād probably still have a blank GitHub without the suggestion. Thatās the reason I can laugh about the offer instead of being enraged. Still, how do you do that? How do you offer such a piss-poor salary to a new hire who you KNOW will get killed on bills just trying to get to work? In retrospect, I think they legitimately didnāt have the money to be starting out new hires at 40-60K. STILL, how does that not come up at some point before all the stuff I just mentioned? Did they think disclosing the salary would make them look unattractive? As compared to disclosing the salary after blowing away an hour of peopleās time? Ugh.
I would have said, āIāll let you know regarding the postion.ā Or something to that effect. In other words, say something to them that they would say to you when you full well know that they will not call you back.
I WOULD have done something like that, but I was so caught off-guard by the low salary. That was less than I was making at my previous job (which was only 10 minutes away). I wasnāt able to find this companyās salaries at the time on a website like Glassdoor, and got hit with that.
Anytime youāre being offered unskilled wages for something like this its hilarious. I took my first job offer outta school, asked for 50k. 6 years later I am making 6 figures now and its only gonna go up.
As for languages, it depends what youāre doing it for. When you need to optimize something in C# vs C++ youāre gonna wish you had that higher level of control.
I know, and thatās been the absolute most painful thing about this whole job hunt and the prospects of my future. I know the good salaries are out there. Iāve interviewed for companies that had them (more interested in the company, of course, but still). Yet, the only ones that seem to want to put me on are the little fly-by-night operations that can can say 22K to start with a straight face. I already have an inferiority complex, so this shit is starting to fuck with my morale. It hurts to only hear back from these rinky-dink places when you know for a fact that just 40-50K (after taxes) would wipe out your student loan debt in a year and let you move into an apartment with no roommate. This field is filled with competition though.
Looks like Iām in the same boat. I make gross roughly $40,000 but have a CS degree and their excuses for not hiring me is the ālack of experienceā. Gimme a break LOL
update on the game Iām working on, you might be able to tell what itās based on. You can probably tell there are a few issues, like blocks destroying the wrong blocks, and the button input is really sensitive.
Itās been a bit of a headache trying to fix null pointer errors and index out of bounds, oh jeez and the collision detection. There were times when block would just teleport into new positions and the game crawl to 2 frames per second.