![]() |
|
#1
|
|||
|
|||
Newbie Problem with cinI'm pretty new at C++, so please work with me. I'm trying to make a small program that calculates the bank interest of an online game that I manage. Here's the complete code for the program, so far:
CPP / C++ / C Code:
It runs pretty smoothly, except for one thing. If you enter in a string where cin places the response into an integer, the script goes crazy. Every time it reaches a cin, it skips over it as if someone had just hit enter. Therefore, it gets caught in an endless loop of printing out the "press i or x" message, and it has to be stopped the hard way. Is there any way to prevent this? Thanks. Last edited by dsmith : 08-Feb-2004 at 11:24.
Reason: Added syntax highlighting
|
|
#2
|
||||
|
||||
|
Hello trs2988. Are there really 2987 other trs's here? J/K
Anyway, you are probably blowing out your memory allocation, by trying to place a string into an integer. I would use fgets in this situation, but for more c++ syntax use getline. It is pretty well covered in this thread. cin and the equivalent c command scanf are kind of weak in these situations. Then your input will always be a string, then when you want to convert it, use a atoi routine. Also, one more comment. You may want to look at using a double for alot of your variables like inbank & interest. There is also a command called atof that will convert a string to a double. Hope this helps. d |
|
#3
|
|||
|
|||
|
Alright, I changed my code, but now I'm having other problems. Here's the new file:
CPP / C++ / C Code:
But now I get the following errors: Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland interest.cpp: Error E2285 interest.cpp 21: Could not find a match for 'istream::getline(string,int)' in function main() Error E2285 interest.cpp 22: Could not find a match for 'atoi(string)' in function main() Error E2285 interest.cpp 25: Could not find a match for 'istream::getline(string,int)' in function main() Error E2285 interest.cpp 26: Could not find a match for 'atoi(string)' in function main() *** 4 errors in Compile *** Does that mean it can't find the functions? Do I have to include another file? I thought the only one for those functions was stdlib.h. |
|
#4
|
||||
|
||||
|
trs2988.
I think the problem is that you are passing both of these functions a string and not a char *. While the string class can do a lot of cool things, it can not replace a char *. You have 2 options. Either convert the string to a char * with the string class or just declare a char * to begin with. (#2 option is by far easier )CPP / C++ / C Code:
PS - Just to take the blame for this, I did say a string in my first post |
|
#5
|
|||
|
|||
Thanks!Thanks! It worked! You have no idea how much that little problem was bugging me.
|
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with my Geforce video card | Shivs | Computer Hardware Forum | 2 | 30-Jan-2004 20:54 |
| problem with php5 cgi installation | fab13 | Apache Web Server Forum | 3 | 19-Nov-2003 09:11 |
| unwanted scrollbar problem | kelly001 | Web Design Forum | 3 | 24-Oct-2003 10:44 |
| folder problem in trees | zuzupus | MySQL / PHP Forum | 23 | 26-Sep-2003 07:32 |
| Code problem (a newbie question) | monkster87 | C++ Forum | 3 | 11-Aug-2003 12:46 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The