C++ :: Rounding And Converting To Integer?

Oct 4, 2014

Ok so I'm in a programming 1 class working with c++. I have the following assignment:

Write a C++ program that:
asks for and accepts the Percentage earned with as a double (i.e. 75.45)
rounds it to an integer (>= .5 rounds up, <.5 rounds down.)
prints the original Percentage and the corresponding Grade and Points exactly as shown below.
prints an error message for any input that is less than 0 or greater than 100.

For example, if user enters 89.4, the program prints out:
Percentage: 89.4% Grade: B Points: 3.00
You must use an if-else statement to do this in your program.
Use fixed and precision output manipulators (see Ch. 3) to display the values with the exact precision shown above.

IMPORTANT:
Each if statement condition should contain only one comparison! read this again
This means code that is similar to this is NOT okay:  if (Percentage >= 80.00 && Percentage <90.00)
This code is not acceptable because the if statement condition above has two comparisons.
(Hint: If you order your if-else chain statements correctly, you will only need one comparison in each.)

I have the program working, but I'm pretty sure I'm not rounding how my professor would like it to. This is my code:

#include <iostream>
#include <cmath>
#include <iomanip>

[Code].....

So my issue here is the rounding, and then theres the converting the double percetnage to an integer. In my next assignment I have to write the program with a switch statement.

View 3 Replies


ADVERTISEMENT

C/C++ :: Converting 8-byte Integer To String Based Integer

Oct 15, 2014

Complete the function myitohex so that its converts 8-byte integer to string based hexadecimals.

Ex: printf("0x%s",myitohex(64,hex)); should printout "0x40" on the screen
char *myitohex(uint8_t i, char *result){
???
return result;
}

I wrote:

#include <stdio.h>
#include <stdint.h>
char *myitohex(uint8_t i, char *result){
*result = i + '0';

[Code] ....

0xp gets printed out which is wrong. I think *result = i + '0'; is wrong. What changes should i do on this row ?

View 4 Replies View Related

C++ :: Converting String Into Integer?

Nov 22, 2013

I'm trying to convert a string into a integer and when I call stoi(line) it causes the program to crash.

int main() {
vector<int> numbers;
int i;
string line;
ifstream myfile ("example.dat");

[Code] ....

The file being read from looks like:

3
12
23
34
12 34
12 12
34 23
23 23

View 1 Replies View Related

C/C++ :: Converting String Into Integer

Sep 26, 2013

struct time
{ int t;
    int h,m,s;    
};  
int main() {
    time t;
    int totalsecond;
    char A[10],B[10];

[Code] ....

It gives error at line no 12.

View 5 Replies View Related

C++ :: Converting String To Integer Array

Apr 17, 2014

For Example, it the entered string is: 0324152397 I want it to get stored in an array like-[0] [3] ...[7]. Secondly the string entered may be of any length that is defined only at run time. So, I also need to calculate string length. How could I do that.

View 2 Replies View Related

C++ :: Converting Integer To Vector And Back

Feb 15, 2012

I'm able to convert an integer to a vector<unsigned char> and back. However, when I try to use a nearly identical function designed for the long long data type, the last byte or two is broken.

Program code:

long long num = 9223372036854775551LL;
cout << "Before: " << num << endl;
vector<unsigned char> data = getBytes(num);
num = getLongLong(data);
cout << "After: " << num << endl;

Code for converting between vector<unsigned char> and long long:

Code:
vector<unsigned char> getBytes(long long value) {
int bytes = sizeof(value);
vector<unsigned char> data(bytes);
for (int i = 0; i < bytes; i++)
data.at(i) = (unsigned char)( value >> ((bytes-i)*8) );

[Code] ....

Output:

Code:
Before: 9223372036854775807
After: 9223372036854775552

Is there something special about long long that would prevent this from working? This problem doesn't happen with int.

View 3 Replies View Related

C++ :: Converting Integer To String Based On Digits

Apr 23, 2013

I want to convert the integer into a string.

int x = 693;
char chararr[max];

In my homework, x is unknown. but don't worry, I wont ask for the full code. I just need the part where you change the int into a string/array of char.

I'm thinking in circles from where to start?

View 2 Replies View Related

C/C++ :: Converting Integer Number To Its 2's Complement Presentation

Jan 28, 2014

I am working on an assignment about converting an integer number to its 2's complement presentation. The binary representation is consisting of a single linked list.

As we all know, the steps of this converting is to taking the reminder of the absolute value, then flipping the 1 to be 0, and the 0 to be 1 in the binary number. And the last step will be to add 1 to the binary number invers.

I wrote a code that implements every thin correctly. However, when I reached the part of adding 1, the program was hanged.

int Absolute; //the first step is to convert the number to the binry reprezentation
Absolute = abs (value);// by take the Absolute value of the negative number, then find the
while (Absolute !=0) //the binary reprezentation {
int res;
res= (Absolute % 2);
pushFront (res);
Absolute /=2 ;

[Code] .....

View 11 Replies View Related

C :: How To Use Rounding Function

Jan 16, 2014

I want to round to the 2nd digit in a float. Such as 1.85458 to 1.85. How would i go about doing this. I currently keep rounding to the closest integer so the above would round to 2.

Code:
#include <stdio.h>
#include <math.h>
int main(void){
float n;

[Code] ....

View 4 Replies View Related

C :: Rounding Function Not Working

Oct 12, 2013

Why the roundf function is not successfully rounding my numbers.

Code:
#include <stdio.h>
#include <math.h>
//function declarations
void CelToFah (int ctemp, int ftemp);

[Code]....

it produces a correct number when called, but it is not a rounded number, the roundf((float)cel) does nothing. I get the same number with and without it.

View 6 Replies View Related

C :: Rounding To The Nearest Cent?

Feb 4, 2014

I'm having trouble with rounding to the nearest cent in this program. It's the only thing that i need. The result i need is just off by one cent. I tried multiplying the interest amount by 100 and adding .5 then dividing all that by 100 but that just made it worse. Here's the code:

Code:
#include <stdio.h>
int main()
{
int count=0;

[Code]....

View 6 Replies View Related

C :: Rounding Of Numbers Using Int Identifier

Apr 23, 2013

I have an assignment for uni which requires the program to ask the user to input a number in for a variable to use in later equations. The assignment specifies that if the number that is input into the program is not an interger that it needs to be rounded UP to the nearest interger. e.g. 2.5 = 3, 5.00001 = 6 etc. i have identified this variable using "int" which i know makes it an interger however it also always rounds the number DOWN to the nearest interger. I was just wondering what the best way to approach this problem was. The only idea i have is to put + 0.99999 at the end of this variable when it is worked out so that if it is not a whole number it will be raised above the next interger and then rounded down however this will not work if there is too many decimal places.

View 6 Replies View Related

C++ :: Rounding To Nearest Half?

Mar 9, 2014

How do I round to the nearest half integer?

Example;
0 to 0.2 = 0
0.3 to 0.5 = 0.5
0.6-0.9 = 1

What's the formula for C++?

I found a way to do it in math but I want to know what I'd have to put in C++. It's (x*2+.5)remove decimal numbers and divide by 2. What do I put in place of "remove decimal"?

For ex; x = 8.4 using (x*2+.5)remove decimal then divide by 2.
8.4 * 2 = 16.8
16.8 + .5 = 17.3
17.3 - decimal = 17
17/2 = 8.5

I want to do something like;

cout << " Half number: << (x*2+.5)remove decimal/2 << endl;

What would I have to put in place of "remove decimal"?

View 4 Replies View Related

C++ :: Rounding Numbers To Hundreds?

Jul 18, 2013

I have made a random number generator to create a number between 1000 and 9000, but I always get numbers with many 10s and units. I want to round them 100s.

Is there any way to do this?

View 4 Replies View Related

C Sharp :: Rounding Up To Nearest 100

Jul 28, 2012

Rounding a numerical figure up to the nearest hundred. E.G.:

256 >> 300
654 >> 700
15 >> 100

I would like to know the formula to enter into Visual Basic 2008. I'm making a calculator Program, and i need a function that rounds up to the nearest 100...

View 5 Replies View Related

C++ :: Dice Simulator - Floats Rounding Up

Oct 28, 2013

I made this dice simulator which basically throws the dice 1 million times and outputs the frequency and percentage average for each side (1 to 6).

Everything is working fine, except my averages (floats) seem to be rounding up, causing 4% being "unassigned" at the end of the million rolls. I'm outputting with a setprecision of 2, but I only get 0's and no fractional numbers.

View 5 Replies View Related

Visual C++ :: Rounding A Number Multiple Of Another

Jul 23, 2013

How would I be able to round a number in multiples of another...

Let's say width is 150
And multiple to be 64...
I want 150 to become 128...
if it was 160 to become 192...

The width number will change and I want to covert it in multiples of the other number example 64... The minimum value will always be the multiple number used...

View 7 Replies View Related

C :: Assign Integer Value To Unsigned Char Array But It Is Not Storing Integer Values

Oct 25, 2013

I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet

Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}

The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.

View 9 Replies View Related

C++ :: Changing Integer Into New Integer With Simple Mathematical Operations?

Jun 15, 2014

changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).

View 4 Replies View Related

C++ :: Converting Int To Char

Feb 20, 2014

Why is this

Code: fFile << (char) (i)p.x, (char) (i)p.y,(char) (i)p.z); not the same as this

Code: printf("%c%c%c", (i)p.x, (i)p.y, (i)p.z);

View 13 Replies View Related

C++ :: Converting Text To Hex

Jan 3, 2013

From my tests I found the problem to be somewhere in the Mantissa part that converts it to binary.

bool xsDLL GetHexFromSF_IEEE754( void* to, Text from, ui08 tSize ) {
from.UpperCase();
int db = 0, dB = 0, dBEnd = tSize, dBLast = ( tSize - 1u ), dbEnd = dBEnd * 8;
ui08 *data = reinterpret_cast< ui08* >( to );
for ( ; dB >= 0; --dB )
data[ dB ] = 0u;

[Code] ....

Adjustments made, still having problems though. After finding a more useful resource [URL] ..... I got the function looking more like it should but am still having problems...

View 1 Replies View Related

C++ :: Converting One Format To Another?

Apr 7, 2013

I am in a c++ class and my group is having a hard time making this work... It keeps saying that 'fileOne.open' isn't working.

#include <iostream>
#include <string>
#include <fstream>

[Code]....

View 3 Replies View Related

C++ :: Converting Char To Hex

Nov 8, 2014

Having trouble converting char to hex.

according to here [URL] ....
ô in decimal is 147 and print ô using alt 147

and here [URL] ....
ô in dec is 244 but prints ⌠ using alt 244

put on console

char c = ô;
cout << (int)c << endl;

prints -109 and print m using alt -109

I am using alt to test char output. Why I'm getting a negative value? Which of the tables are correct? I have string of char that I want to print in hex. I get a hex string but the hex value don't correspond to any of the two tables on the websites because the console converts special char to negative values.

View 2 Replies View Related

C++ :: Converting CString To Int?

May 16, 2013

I am having problems with converting a CString to an int and than doing checks on the int to see if it is in a particualr range.Below is what I am doing.

CString numstr = "28"
int num = atoi(numstr);
BOOL valid = TRUE;
if(num < -32,768 {
valid = FALSE;
}

For some reason when running the above code the if statement is executed but it should not be becasue 28 is not less than -32,768. Why this is happening, I am not seeing the reason for this at all!! The num variable is being assigned the correct value.

View 5 Replies View Related

C++ :: Converting Strings In Application

Jun 8, 2014

I am having trouble converting strings in my application.

This is the code that is eventually getting called, the first parameter takes a LPCWSTR

Code:
hr = m_pGraph->RenderFile(m_filePath, NULL); // takes LPCWSTR This is where the trouble begins.

The compiler shows that sResult is proper but after I try to convert a std::string to a wchar it prints garbage

Code: const wchar_t* m_filePath = (wchar_t *) oDb->GetNext().c_str();

Code:
std::string CSqlLiteDatabase::GetNext() {
sqlite3_stmt *oStmt = nullptr;
std::string sql = "SELECT path || filename FROM table WHERE id = 1";
char message[255];

[Code] ....

View 6 Replies View Related

C :: Converting Netpay To String

May 18, 2014

Code:
void convertNetPay(float netPay, char *netPayString){
int numHuns, numTens, numOnes;
char OnesTable[9][8]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
char ResultString[10+1];
char TensTable[9][8] = {"Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};
char TeensTable[9][10] {"Eleven","Twelve","Thirteen","fourteen","fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
float cents;

[Code] ....

I have to convert my netpay which is a float to a string So if I input a value of say, 356.26 it should output "the sum of three hundred fifty-six and 26/100 dollars" . My program function works for the sum of three hundred but after that it spits out garbage.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved