473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to print a multiple characters?

i have a really question that's been bugging me for a little too long now. so i want to print this, where 'grade' is a char

Expand|Select|Wrap|Line Numbers
  1.  if(average >= 100) { grade = 'A+' ; }
  2.         else if(average >= 90 && average < 100) { grade = 'A' ; }
  3.         else if(average >= 80 && average < 90) { grade = 'B' ; }
  4.         else if(average >= 60 && average < 80) { grade = 'C' ; }
  5.         else if(average >= 50 && average < 60) { grade = 'D'; }
  6.         else if(average < 50) { grade = 'F'; }
  7.  
everything works except for A+ which prints as +. what seems to be the problem here?
Mar 6 '10 #1
3 5944
Adam01
83
Your storing 2 characters in one place.
Where as ' ' can only hold one character.

A quick fix would be to define grade as
char grade[2];

Then use grade[0] = 'A' for single grade letters, and then
grade[0] = 'A';
grade[1] = '+';
Mar 6 '10 #2
Banfa
9,065 Expert Mod 8TB
But be aware if you store grade as 2 characters

char grade[2];

Then you have exactly that, an array of 2 characters, both of which may or may not hold a value. You do not have a string because a string requires a zero '\0' terminating character and you do not have room in you array for one. To print this you will need to be something interesting using %c twice in a printf statement.

However what you could do instead is declare an array of 3 characters

char grade[3];

and then actually store a string, it might only be a string of 1 or 2 characters but you would then be able to use %s to print it out regardless of the number of characters it contains.

Of course you can't assign with = to a string (in C) so you would have to use strcpy.
Mar 6 '10 #3
u also write that if(avg>=100){
cout<<"A+"<<endl;
}
it ll be better for u
Mar 6 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

48
by: David J Patrick | last post by:
I'm trying to rewrite the CSS used in http://s92415866.onlinehome.us/files/ScreenplayCSSv2.html. using the w3.org paged media standards as described at http://www.w3.org/TR/REC-CSS2/page.html ...
3
by: Paul Watson | last post by:
It is clear that just using 'print' with variable names is relatively uncontrollable. However, I thought that using a format string would reign the problem in and give the desired output. Must...
5
by: Tom Louchbaum | last post by:
When I preview my Access 2000 Report it looks fine. When I Email the report using DoCmd.SendObject acSendReport, "Report", acFormatRTF, "To Address", , , "Subject", , False the resulting...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: metzger | last post by:
I am using the function listed below to handle characters events in SAX. It does not handle multiple sequential calls to this function correctly. For example, I am getting "2 4 816 32 64" as a...
2
by: Phoe6 | last post by:
print and softspace in python In python, whenever you use >>>print statement it will append a newline by default. If you don't want newline to be appended, you got use a comma at the end (>>>print...
9
by: xprotocol | last post by:
Trying to print a web page that scrolls to right a bit. I would think IE would page break by default when it gets to the end of the sheet, but it doesn't, it just cuts off the print. This only...
5
by: kuratkull | last post by:
Hello, *************** import urllib2 import re import string import sys url = "http://www.macgyver.com/" request = urllib2.Request(url)
58
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.