GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Feb-2004, 22:09
teimu teimu is offline
New Member
 
Join Date: Feb 2004
Posts: 5
teimu is on a distinguished road

noob question of % escape sequence.


Hello all. I just joined and this will be my first post here at GID.
I was having a problem on getting a percent sign(%) to print in front of an escape sequence. %f is supposed to represent some percent with decimals
Heres what i what i have typed.
Code:
printf("Thats a %.0f increase.",perg);
it should print
Code:
Thats a (example number) increase.
i want it to be a %whatever. so i make this change.
Code:
printf("Thats a %%.0f increase.",perg);
this for some reason does not make sense to the complier. when i run the prog, i get...
Code:
Thats a %.0f increase.
when there should be a valid variable in there describing percent. What should i do?
Please open my file for a more in depth view of what im trying to do. Anyway. i just got into C and am loving it. I hear its a fundamental language that will reinforce on my learning of others, which i plan to do. im really excited about it. if you would like to comment on my code, it would be my honor. i am probably not the most effiecient programmer, so any advice on condensing it or such would be great. My math isnt probably that good either, im only in 9th grade Algebra I, im not sure if the ways ive chosen are the best.
Thanks
Attached Files
File Type: txt stock.txt (2.5 KB, 6 views)
  #2  
Old 24-Feb-2004, 22:41
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Your beginning of your post is confusing. When you post an example, like
Code:
Thats a (example number) increase.
I assume you realy meant something like
Code:
Thats a 23 increase.
Is this what you are seeing from your printf()?

Any time you use "%%" in a printf, you will print a single % symbol. So if you want the message
Code:
Thats a 25% increase.
you want
Code:
printf("Thats a %.0f%% increase.",perg);
  #3  
Old 25-Feb-2004, 11:35
ObiWan506 ObiWan506 is offline
New Member
 
Join Date: Feb 2004
Posts: 9
ObiWan506 is on a distinguished road
Teimu, I think what also is happening is that you don't properly attach the "%f" to the variable you want. Since the compiler doesn't see a variable attached to "%f" it leaves it as %f. The only thing I see wrong is that in your printf statement, you must include the "&" symbol, so your code should look like this:

printf(That's a %.0f%% increase.", &perg);

Try that and see if it works
  #4  
Old 25-Feb-2004, 13:44
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ObiWan506
Teimu, I think what also is happening is that you don't properly attach the "%f" to the variable you want. Since the compiler doesn't see a variable attached to "%f" it leaves it as %f. The only thing I see wrong is that in your printf statement, you must include the "&" symbol, so your code should look like this:

printf(That's a %.0f%% increase.", &perg);

Try that and see if it works
Oh, Obiwan, the Force may be strong with you, but the dark side has muddled your thinking!

perg should be defined as a double or float, so the & symbol is not necessary.
  #5  
Old 25-Feb-2004, 14:53
ObiWan506 ObiWan506 is offline
New Member
 
Join Date: Feb 2004
Posts: 9
ObiWan506 is on a distinguished road
Yes, the Force is strong with me but much to learn I still have! I am still a intermediate programmer so I am still learning certain things. Okay, so maybe the "&" isn't needed, but does the space between " and the , matter. For instance, Teimu posted - printf("That's a %f increase.",perg);.....
But shouldn't he have a space like this - printf("That's a %f increase.", perg);....or does the space matter?

<--- I just put this in because I thought it was funny,
  #6  
Old 25-Feb-2004, 15:10
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Usually, programmers add spaces for better readability, but they are not required by the compiler. You could write an entire program in one line, but no good programmer would ever actually do that. Just to be clear, whitespace is not totally ignored. You couldn't write a function prototype like this and get away with it:
CPP / C++ / C Code:
intmain();
C would read that all as one word, but you could do something like this...
CPP / C++ / C Code:
#include <iostream.h>
int main(){cout<<"thisisabadideabecauseyoucan\'treadit\n";return 0;}
  #7  
Old 25-Feb-2004, 19:00
teimu teimu is offline
New Member
 
Join Date: Feb 2004
Posts: 5
teimu is on a distinguished road
ahhh, much better. thanks WaltP ,ObiWan506, aaroncohn. really apprieciate it. works now.
however. im running into a new problem. ill post it here to save space instead of a new thread.
okay. now that my percent sign shows up completely, it looks fine and dandy. i would now like the user to be able to choose if he wants to run the program again. he can, but if he types 'y' as opposed to "Y", it will determine it false, and move onto else. look at this
Code:
printf("\nStart again? (Y/N)\n"); restart=getch(); if(restart=='Y' && 'y') { printf("this is supposed to start again..."); } else { printf("user requests quit"); } getche(); }
including declaring "restart" all's ive done was added this chunk at the end of the file in my first post. the thing is im not even sure if thats how you make sure both conditions are checked (with the "&&" thing.) ive just seen that figure around and it looks like it would add a condition to the if statement. sorry, noob curiosity.
also would you know how i would make the program go back up to the start again, if, of course the user chooses to do so. i just learned the 'for' function, but that was like
Code:
for(starting ; if_this_is_true ; do_this)
ive used it to count for me(which is absolutely pointless at this juncture) but i think you can use "for" to somehow actually loop my program to a certain spot, under the users consent. thanks. hope im not troubling you to much!
  #8  
Old 25-Feb-2004, 19:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Logical operator


Try this:

Code:
if ((restart == 'Y') || (restart == 'y')) { ... stuff to do whenever it's 'Y' or 'y'... } else { ... stuff to do whenever it's not 'Y' and also not 'y'... }
The || operator works like this:

If the first condition is true, then execution proceeds
(the other condition is not checked).

If the first is not true, then the second is checked. If
the second is true, then execution proceeds.

The result is that, if neither condition is true, the
"else" block is executed.

Dave



Quote:
Originally Posted by teimu
ahhh, much better. thanks WaltP ,ObiWan506, aaroncohn. really apprieciate it. works now.
however. im running into a new problem. ill post it here to save space instead of a new thread.
okay. now that my percent sign shows up completely, it looks fine and dandy. i would now like the user to be able to choose if he wants to run the program again. he can, but if he types 'y' as opposed to "Y", it will determine it false, and move onto else. look at this
Code:
printf("\nStart again? (Y/N)\n"); restart=getch(); if(restart=='Y' && 'y') { printf("this is supposed to start again..."); } else { printf("user requests quit"); } getche(); }
including declaring "restart" all's ive done was added this chunk at the end of the file in my first post. the thing is im not even sure if thats how you make sure both conditions are checked (with the "&&" thing.) ive just seen that figure around and it looks like it would add a condition to the if statement. sorry, noob curiosity.
also would you know how i would make the program go back up to the start again, if, of course the user chooses to do so. i just learned the 'for' function, but that was like
Code:
for(starting ; if_this_is_true ; do_this)
ive used it to count for me(which is absolutely pointless at this juncture) but i think you can use "for" to somehow actually loop my program to a certain spot, under the users consent. thanks. hope im not troubling you to much!
  #9  
Old 25-Feb-2004, 20:42
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by teimu
ahhh, much better. thanks WaltP ,ObiWan506, aaroncohn. really apprieciate it. works now.
however. im running into a new problem. ill post it here to save space instead of a new thread...
Welcome.

Dave's response about the if() is accurate.

As for the loop, a do-while() loop is made for this type of true/false loop. for() loops are best used for range loops -- 1 thru 10, 'A' thru 'F', etc.
At the top of the loop, simply put do. At the end add while() statement and in the parens put the expression Dave used in his if(). Then all you will need is the prompt and the character input at the end. The while takes places of the if.
CPP / C++ / C Code:
do
{
  -- code --
    get character
} while (conditional);
  #10  
Old 25-Feb-2004, 22:39
teimu teimu is offline
New Member
 
Join Date: Feb 2004
Posts: 5
teimu is on a distinguished road
haha. YYEESSSSS. you all must know the feeling you get when debug some code. i didnt understand that you had to take out the else portion hanging on back there, even tho you wrote it right out for me
Quote:
Originally Posted by WaltP
Then all you will need is the prompt and the character input at the end.
woohoo. my code now meets my standards of what i want it to do and i learned alot. thanks guys
 
 

Recent GIDBlogWriting a book 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
question of practice magiccreative C++ Forum 1 06-Feb-2004 08:17
a C input question tmike C Programming Language 1 16-Sep-2003 03:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:20.


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