Keep running program and 'int' problem

This is a discussion on Keep running program and 'int' problem within the C++ Programming forums, part of the General Programming Boards category; It runs just fine the way it is in my previous post, but it won't exit. Of course, as I ...

  1. #16
    coder
    Join Date
    Feb 2008
    Posts
    127
    It runs just fine the way it is in my previous post, but it won't exit.
    Of course, as I told you: it runs fine because while (yn = 1) assigns 1 to yn every loop cycle.
    Running the program, even typying in any different number, yn will be changed to 1 again.

    I set yn = 1 just before the while statement and after the char yn; statement, and it doesn't allow me to rerun the program at all.
    You can't rerun the program because you compare (yn == 1) when yn is a char.
    A char is interpreted as a character, not a numeric value, so the comparing won't be true when you type in 1.

    Two solutions:
    1: Declare yn as an int or a bool.

    2: Compare yn with a char -> (yn == "r")
    and of course change the sentence to "Enter 'r' to rerun program: "

  2. #17
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by carlorfeo View Post

    2: Compare yn with a char -> (yn == "r")
    and of course change the sentence to "Enter 'r' to rerun program: "


    (yn=='r')
    actually. Single quotes for char and double quotes are for string.
    Last edited by abh!shek; 02-07-2008 at 08:13 PM.

  3. #18
    coder
    Join Date
    Feb 2008
    Posts
    127
    Thanks abk, you are right.
    Of course an assignement follows the same rule: char yn = 'r';
    Using double quotes you wil get error: invalid conversion from 'const char*' to 'char'

    Quote Originally Posted by carlorfeo
    You can't rerun the program because you compare (yn == 1) when yn is a char.
    A char is interpreted as a character, not a numeric value, so the comparing won't be true when you type in 1.
    Well, sorry, I really wasn't right saying that!
    You can assign a number to a char, but only if you know how to handle it.

    e.g.

    char ch = 49;
    char ch = '1';

    these two lines do the same thing, since the ascii code of the character '1' is 49.

    Indeed using char's to handle numbers is not a good way
    e.g.

    char number = 97;
    cout << number << endl;

    the code above will print a (ascii code = 97)

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    Thanks for all your help. It's helped a lot. I've got it working perfectly now. It's going to be more than a wind chill calculator. I'll upload it when i'm finished just in case anybody wants to see it. (source code and exe)

    Edit:

    Here is the source code and exe: http://rapidshare.com/files/90448900/Calc_thingy.zip

    I used dev-c++, so the .dev project file is there too.
    Last edited by willrs2; 02-09-2008 at 10:30 AM.

  5. #20
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by willrs2 View Post
    Thanks for all your help. It's helped a lot. I've got it working perfectly now. It's going to be more than a wind chill calculator. I'll upload it when i'm finished just in case anybody wants to see it. (source code and exe)
    You should actually hide your source code, so other students may not cheat

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Or I got a better idea - watermark the thing so that if anyone steals it, you know who the culprit is!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Feb 2008
    Posts
    11
    True, lol. But I just made the program to learn, it's not a school project. I plan on taking programming next year, though, so thanks for the advice. This is a popular board.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. My Allegro Program will not run...
    By Vicious in forum Game Programming
    Replies: 17
    Last Post: 06-14-2002, 12:49 AM
  5. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 10:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21