Archived

This topic is now archived and is closed to further replies.

Penguinzchaseme

Code problem

Recommended Posts

This code multiplies the number of each letter in a string(a=1 z=26) and checks to see if the modulus of each string divided by 47 is equal. #include <iostream> using namespace std; int main() { char alpha[27]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char *UFO, *Group; int n, b=1, c=1; cin>>*UFO; cin>>*Group; for(n=0; alpha[n];n++); { while(*UFO){ if(*UFO=alpha[n])*UFO=n+1; else UFO++; } while(*Group){ if(*Group=alpha[n])*Group=n+1; else Group++; } } while(*UFO){ b=b*(*UFO); UFO++; } while(*Group){ c=c*(*Group); Group++; } if((b%47)==(c%47)){ cout<<"Go"; } else cout<<"Stay"; return 0; } but it only allows me to input the first string(*UFO)-any ideas? [edited by - Penguinzchaseme on April 30, 2003 11:18:55 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Turn on all compiler warnings. Heed them. For example, "use of uninitialized variable" on line 10.

Share this post


Link to post
Share on other sites
andy_fish    122
yay for homework.

I think the line "cin>>*UFO;" should be "cin>>UFO;" .. because otherwise it will just get one character from cin.

but instead of using char*, you should be using std::string. Yes, using STL is painful at first, but it is worth it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Back in high school on those POS computers that would happen to me sometimes too. The way i got around it was i put some sort of delay between the 2 cin>> s because when I pushed [enter], both of the cin>> took the enter. So i did a cheap little for(i=0;i<500;i++); in there.

they where crappy celerons with boreland 4 on them. That what you have???

God i hated those computers...

I dont think that a uninitialized varaible would cause that problem because you are setting a variable.

Another thing I noticed is that you are doing

cin>>*charPrt;
but i remeber doing
cin>>charPtr; without the * because I think you are inputting the address and not the text mb???

Those are the only ideas i have.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
hey, i have a question for you. Why do i get mem leaks with std::string? Is there a common mistake people make?

(same person as above)

Share this post


Link to post
Share on other sites
Sneftel    1788
quote:
Original post by Anonymous Poster
Back in high school on those POS computers that would happen to me sometimes too. The way i got around it was i put some sort of delay between the 2 cin>> s because when I pushed [enter], both of the cin>> took the enter. So i did a cheap little for(i=0;i<500;i++); in there.

they where crappy celerons with boreland 4 on them. That what you have???

God i hated those computers...

I dont think that a uninitialized varaible would cause that problem because you are setting a variable.

Another thing I noticed is that you are doing

cin>>*charPrt;
but i remeber doing
cin>>charPtr; without the * because I think you are inputting the address and not the text mb???

Those are the only ideas i have.


Ye gods. Please, just... just ignore everything that AP just said. About 90 percent of it is wrong. Yes, your problem is an uninitialized variable: Your pointer does not point to valid memory at the time it is dereferenced. a std::string will enable you to not worry about this.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites