GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Feb-2004, 10:57
trs2988 trs2988 is offline
New Member
 
Join Date: Feb 2004
Posts: 6
trs2988 is on a distinguished road

Newbie Problem with cin


I'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:
#include <stdio.h>
#include <iostream.h>

int main() {
string response;
int inBank;
int millions;
int interest;
int days;
int origDays;
while(1) {
cout << endl << "Type I to calculate interesting, or X to exit: ";
cin >> response;
response = response[0];
if(response == "X" || response == "x") break;
if(response == "I" || response == "i") {
	inBank = 0;
	cout << "Amount in bank: ";
	cin >> inBank;
	if(inBank <= 0) continue;
	cout << "Days: ";
	cin >> days;
	if(days <= 0) continue;
	origDays = days;
	for(;days>0;days--) {
	cout << "--After " << origDays - days + 1 << " Days(s)--" << endl;
	// This is the way I figure out interest in my game, ignore it.
    millions = inBank / 1000000;
	if(millions < 1) millions = 1;
	interest = inBank * ((3 / sqrt(millions)) / 100);
	// End interest calculation
	cout << "Amount of interest: " << interest << endl;
	inBank += interest;
	cout << "Total in bank: " << inBank << endl;
	}
}
if(response != "X" && response != "x" && response != "I" && response != "i") cout << "Invalid choice." << endl;
}
cout << "Exiting." << endl;
}


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  
Old 08-Feb-2004, 11:34
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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  
Old 08-Feb-2004, 11:50
trs2988 trs2988 is offline
New Member
 
Join Date: Feb 2004
Posts: 6
trs2988 is on a distinguished road
Alright, I changed my code, but now I'm having other problems. Here's the new file:

CPP / C++ / C Code:
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>

int main() {
string response;
string temp;
int inBank;
int millions;
int interest;
int days;
int origDays;
while(1) {
cout << endl << "Type I to calculate interesting, or X to exit: ";
cin >> response;
response = response[0];
if(response == "X" || response == "x") break;
if(response == "I" || response == "i") {
	inBank = 0;
	cout << "Amount in bank: ";
	cin.getline(temp,10);;
	inBank = atoi(temp);
	if(inBank <= 0) continue;
	cout << "Days: ";
	cin.getline(temp,3);
	days = atoi(temp);
	if(days <= 0) continue;
	origDays = days;
	for(;days>0;days--) {
	cout << "--After " << origDays - days + 1 << " Days(s)--" << endl;
	// This is the way I figure out interest in my game, ignore it.
    millions = inBank / 1000000;
	if(millions < 1) millions = 1;
	interest = inBank * ((3 / sqrt(millions)) / 100);
	// End interest calculation
	cout << "Amount of interest: " << interest << endl;
	inBank += interest;
	cout << "Total in bank: " << inBank << endl;
	}
}
if(response != "X" && response != "x" && response != "I" && response != "i") cout << "Invalid choice." << endl;
}
cout << "Exiting." << endl;
}


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  
Old 08-Feb-2004, 12:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
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:
char input[MAX_SIZE+2];   //MAX_SIZE should be defined by #define

cin.getline(input,MAX_SIZE);
atoi(input);

PS - Just to take the blame for this, I did say a string in my first post
  #5  
Old 08-Feb-2004, 12:05
trs2988 trs2988 is offline
New Member
 
Join Date: Feb 2004
Posts: 6
trs2988 is on a distinguished road

Thanks!


Thanks! It worked! You have no idea how much that little problem was bugging me.
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 14:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.