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;
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.
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 09:32 PM.
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.
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?
#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;
}
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.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
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
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.
Bookmarks