Re: Complies but doesn't run
Quote:
Originally Posted by
Rayman452
Hi, I'm completely new to programming. That being said, one of the programs I made for an assignment seems to compile, but when I run it I get an error.
Please give us more details on the error.
Also, since you're new to programming, you need to realize that just because a program compiles, it doesn't guarantee that the program will do what you intended it to do. You still have the logical part of the whole equation to overcome. If you want a program to add two numbers, and instead you mistakingly coded to subtract them, the program may have been correct in terms of syntax, but it is incorrect in that it didn't perform the job requested.
Also, you must learn to use your debugger, as this will let you know what each variable is at each step of the program.
Regards,
Paul McKenzie
Re: Complies but doesn't run
As to your error, where do you assign "digit" a value? You declare it, but you don't assign it any value. You input a value into number, and then for some reason, you assume that "digit" also has this same value as a string. C++ doesn't work that way -- variables must be assigned a known quantity for them to have something to work with.
Regards,
Paul McKenzie
Re: Complies but doesn't run
But you have to give him props. Usage of stl, correct signature of main and code tags :thumb:.
Re: Complies but doesn't run
Quote:
Originally Posted by
STLDude
But you have to give him props. Usage of stl, correct signature of main and code tags :thumb:.
Yes, RayMan452 has picked a good book or teacher to learn from, unlike most newbies to C++ that post here.
Regards,
Paul McKenzie
Re: Complies but doesn't run
haha i know how you feel
what you need to do is figure out a way to make digit and number equal, right now nothing is being entered,
I tried
Code:
string digit;
int number;
cin >> digit >> number;
But that means you have to enter the number 2 times, but if you do, it works.
Re: Complies but doesn't run
The best strategy for user input is to get all input as strings then parse from the string whatever you need. Parsing ints from strings is very easy with stringstreams and getting input from a user as a string makes your code robust. It is unlikely to crash even if the input comes from a dose of keyboard slapping.
Re: Complies but doesn't run
I'm taking introduction to programming in my university, using Cay Horstmann's "C++ for Everyone" as my textbook. I'm also using Microsoft's visual studio 2008 as my compiler. I'm trying to write a code to insert a comma in the thousands spot.
If I enter a value less than 100, it works. If I get rid of my if statements, the code manages to work. However, with the if statement I seem to get a debug error, where "this application has requested the run time to terminate it in an unusual way. Please contact the application's support team for more information"
How do I set it up so that I only have to input the number once for it to be assigned to the string digit, and the int number? I need it to be assigned to an int number so that the if statements can check it and carry out the function.
What is parsing from the strings and how do I do it?
Re: Complies but doesn't run
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
bool StringToInt(const string &s, int &i)
{
istringstream myStream(s);
if (myStream>>i)
return true;
else
return false;
}
int result;
int number;
string digit;
int main()
{
cout<<"Please enter an integer greater than or equal to 1000: ";
cin >> digit;
if (StringToInt(digit, result))
{
number = result;
}
else
{
cout << "The Number you put in is not an iterger" <<endl;
}
if ( number >= 1000 )
{
string last_three_number = digit.substr(digit.length()-3);
string first_three_number = digit.substr(0,3);
cout << "Your integer with commas is " <<first_three_number + "," + last_three_number <<endl;
}
else if (number < 1000)
{
cout << "Please enter a higher integer value" << endl;
}
return 0;
}
WHEW that was strange.
Heres where i found the converter
http://faq.cprogramming.com/cgi-bin/...wer=1046996179
FYI You should comment your work more
Re: Complies but doesn't run
Quote:
Originally Posted by
H aun
[CODE]
FYI You should comment your work more
Very simple code like that doesn't need to be cluttered with comments.:mad:
Re: Complies but doesn't run
:o i think its a good idea to comment a fair ammont, i comment on every line most of the time. But if you do thats ok....
Re: Complies but doesn't run
Quote:
Originally Posted by H aun
i think its a good idea to comment a fair ammont, i comment on every line most of the time.
It is a good idea to use descriptive names for identifiers so that the code is "self-commenting". If you need to comment on almost every line, it either means that you have rather undescriptive names, or your comments are redundant.
Re: Complies but doesn't run
Most of them are (haha) but thats ok. I like to know every thing on the spot with good description. Mainly i do it so that people know what the heck they are looking at when i need help, or for when i go back to backed up files so i know what it does
Re: Complies but doesn't run
Quote:
Originally Posted by
H aun
:o i think its a good idea to comment a fair ammont, i comment on every line most of the time. But if you do thats ok....
Then either you write very non-intuitive code or you waste a lot of time. If you use self explanatory variable and function names, and keep your code nicely compartmentalized and use proper indentation and brace matching, there should be very little need to comment almost every line.
Code:
int a = 1; //set a to 1
if( a == 1) // see if a is equal to 1
{
...
}// end if
Comments like that are just plain goofy.
If you write your code properly, you'll know what it does without having to read comments.
Re: Complies but doesn't run
Thanks for settin down the law man. good job, wip those programmers into shape. No useless commenting! it clogs up your code
Re: Complies but doesn't run
any way i think its a good idea for a new programmer to get into the habit of commenting their code. but thats me.
So! Rayman any other problems?
Another thing you might want to do is use <cstdio> (include it), it lets you use Command Prompt comands, pause, cls, ect. used by system("commnd"), example system("pause")
i use pause A LOT!!!!
Really makes things easier
Re: Complies but doesn't run
Quote:
Originally Posted by H aun
any way i think its a good idea for a new programmer to get into the habit of commenting their code.
It is, but one must also learn not just to comment but to comment appropriately.
Quote:
Originally Posted by H aun
Another thing you might want to do is use cstdio (include it), it lets you use Command Prompt comands, pause, cls, ect. used by system("commnd"), example system("pause")
i use pause A LOT!!!!
Really makes things easier
It does? It sounds like you are simulating a debugger, but back in post #2 Paul McKenzie already suggested that Rayman452 learn how to use a debugger.
Re: Complies but doesn't run
no, it works in the program
Re: Complies but doesn't run
Quote:
Originally Posted by H aun
no, it works in the program
How does it "make things easier"? I do not see how the use of such platform dependent external utilities will benefit Rayman452's program.
Incidentally, std::system() is found in <cstdlib>, not <cstdio>.
Re: Complies but doesn't run
well with out the system("pauses") you need to go into command prompt, then navigate to the executibal and run it.
You put the pauses in places (well at least for me) when something is read out and nothing is being entered.
example
Code:
void errorcm()
{
cout << ent << " Is Not A Command" << '\n' ;
system("pause");
system("cls");
help();
}
that paused it after displaying
Quote:
"asdf" is not a command
and the cleared it up, so the stuff in help() could be displayed more cleanly
also im using Dev C++ and system("") is the function for that, and the <cstdio> is not the system stuff, thanks for correcting me.
Re: Complies but doesn't run
Quote:
Originally Posted by H aun
well with out the system("pauses") you need to go into command prompt, then navigate to the executibal and run it.
That is the way a command line program is supposed to be run (or you could run it from say, a batch file). Rayman452 is using Visual Studio 2008, and that IDE is likely to keep the console window open when running the program via the IDE. Even if it does not, inserting a break point at the end of the program should be a suitable workaround.
Re: Complies but doesn't run
I dipped into Visual studios, always kicked my butt. might dip back in there