Solved

C++ operating string

Posted on 2000-02-23
7
241 Views
Last Modified: 2010-04-02
I'm going crazy trying to get my remainders to display I'M using the right operators but its still coming back a whole number.
How do I get correct Quotient responce.
 Quotient=Int_1%int_2;
 5%2 does not =2 it equals 2 etc/etc
0
Comment
Question by:wolv491
7 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2550230
Don't understand:
int i, j, r;
r = i % j;
0
 
LVL 1

Expert Comment

by:Binder
ID: 2550232
 I don´t know if I understand you correctly but are you trying to
do some divisions out of a string? A bit of code of what you are doing
may help.
0
 

Author Comment

by:wolv491
ID: 2550254
Edited text of question.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:arnond
ID: 2550305
if you want real decimal numbers (like 1.23456...) you need to use floats. Either use floats instead of ints or cast them.

float fl_i,fl_j,q;
int int_i,int_j;

q=fl_i%fl_j;
or
q=((float) (int_i))/((float)(int_j));

Hope that's what you want,

Arnon David.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2550361
wolv491, please do not edit the question once a dialog has started, it makes it impossible to follow.

>> 5%2 does not =2 it equals 2 etc/etc
It should equal 1

We need to know what it is you are doing.  Can you explain this a little more clearly and/or post some code>
0
 
LVL 22

Expert Comment

by:nietod
ID: 2550382
>> correct Quotient responce.
>> Quotient=Int_1%int_2;
pehaps you are mixing up / and %.

/ does division and returns the quotient.  when deailing with ints, it returns only the integer portion fo the quotient, like

5/2 = 2
6/2 = 3
7/2 = 3
8/2 = 4

% does division and returns the remainder.  like

5%2 = 1
6%2 = 0
7%2 = 1

5%3 = 2
6%3 = 0
7%3 = 1
8%3 = 2
9%3 = 0
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 2552625
Is it just me, or does the question title have nothing to do with the question.

And the question itself doesn't make sense.

>I'm going crazy trying to get my
>remainders to display

But your variable is called 'quotient' and you want remainders .. no wonder you're going crazy

>I'M using the right operators but its
>still coming back a whole number.

That is because quotients and remainders ARE whole numbers

>How do I get correct Quotient
>responce.

Which is it .. quotient or remainder?

> Quotient=Int_1%int_2;
> 5%2 does not =2 it equals 2 etc/etc

its not 2 .. it is two .. what does that mean?

All I can thing of is that you REALLY want to divide 5 by 2 and get 2.5 OR you want to get .5

Here's how
int i,j,k;
double d,r;
i = 5;
j = 2;
d = i/(double)j; // d is now 2.5
r = (i%j)/double(j) // r is now .5

OK?




0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question