-
January 23rd, 2009, 10:09 PM
#1
Complies but doesn't run
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. I can't figure out why its happening. Could someone point me in the right direction?
PHP Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number;
cout<<"Please enter an integer greater than or equal to 1000: ";
string digit;
cin>>number;
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;
}
-
January 23rd, 2009, 10:25 PM
#2
Re: Complies but doesn't run
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
-
January 23rd, 2009, 10:29 PM
#3
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
Last edited by Paul McKenzie; January 23rd, 2009 at 10:32 PM.
-
January 23rd, 2009, 11:24 PM
#4
Re: Complies but doesn't run
But you have to give him props. Usage of stl, correct signature of main and code tags .
-
January 24th, 2009, 10:02 AM
#5
Re: Complies but doesn't run
Originally Posted by STLDude
But you have to give him props. Usage of stl, correct signature of main and code tags .
Yes, RayMan452 has picked a good book or teacher to learn from, unlike most newbies to C++ that post here.
Regards,
Paul McKenzie
-
January 24th, 2009, 11:26 AM
#6
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.
-
January 24th, 2009, 11:48 AM
#7
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.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
January 24th, 2009, 12:19 PM
#8
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
-
January 24th, 2009, 12:39 PM
#9
Re: Complies but doesn't run
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.
-
January 24th, 2009, 12:58 PM
#10
Re: Complies but doesn't run
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....
-
January 24th, 2009, 01:41 PM
#11
Re: Complies but doesn't run
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. 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.
-
January 24th, 2009, 12:09 PM
#12
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?
-
January 24th, 2009, 01:00 PM
#13
Re: Complies but doesn't run
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.
-
January 24th, 2009, 01:04 PM
#14
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
-
January 24th, 2009, 01:53 PM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|