C++ Diver Program help

This is a discussion on C++ Diver Program help within the C++ Programming forums, part of the General Programming Boards category; Ok so I am having some issues with my diver program, my issue is that when I use my if ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    39

    C++ Diver Program help

    Ok so I am having some issues with my diver program, my issue is that when I use my if statement to validate input it only does it one time so if you write the input again it does not correct it the second time. Here is some sample code.

    Code:
    if (Score <= 0 || Score >= 11) 
    {
    cout << "You entered an incorrect value (1-10)" << endl;
    cout << "Enter the score given by judge # " << Judge << endl;
    cin >> Score; 
    }
    what is it that I am doing wrong in here? I need it to continue to validate the input if it is wrong more then once.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Read Lesson 3: LoopsThat will show you how to o it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by AndrewHunter View Post
    Read Lesson 3: LoopsThat will show you how to o it.
    Ok so your saying that I have to use a loop instead of an if statement?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by zonen View Post
    Ok so your saying that I have to use a loop instead of an if statement?
    No, you should use loop AND if statement. Well, you could also use only one loop. But it is very likely that you will have to use both. And rememeber that your loop should execute at least one time.
    I never put signature, but I decided to make an exception.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,608
    You need to familiarize yourself with the basics of the language first. Get a good book (such as Accelerated C++) or at the very least, go through the tutorials on the site.
    Then start on a flowchart to write down the flow of your program.
    Finally, translate that to code.
    It will go much smoother, and in the future, it may elide the usage of a flowchart entirely. But that's only when you feel you are good enough to make do without it.
    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.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by zonen View Post
    Ok so your saying that I have to use a loop instead of an if statement?
    I am saying you should read the link I posted for you. As for input verification, you can do something like below. (Note: the tutorial covers all the information you need in order to understand loops and uses for them).
    Code:
    #include <iostream>
    
    int main(void){
    
    	int score;
    
    	std::cout<<"Enter score given by judge(1-10):";
    	std::cin>>score;
    
    	while(score <=0 || score >= 10){
    		std::cout<<"You entered a incorrect value."<<std::endl;
    		std::cout<<"Enter score given by judge(1-10):";
    		std::cin>>score;
    	}
    
    	/*whatever else you are doing in your program. You can use
    	while loops like above for input verification.*/
    
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Thanks for the help, I read through loops and made changes after realizing that the if statement will not continue to "Loop" which is why my issue was there. Thanks for the guidance as this has atleast got me a step further in this program.


    Quote Originally Posted by AndrewHunter View Post
    I am saying you should read the link I posted for you. As for input verification, you can do something like below. (Note: the tutorial covers all the information you need in order to understand loops and uses for them).
    Code:
    #include <iostream>
    
    int main(void){
    
    	int score;
    
    	std::cout<<"Enter score given by judge(1-10):";
    	std::cin>>score;
    
    	while(score <=0 || score >= 10){
    		std::cout<<"You entered a incorrect value."<<std::endl;
    		std::cout<<"Enter score given by judge(1-10):";
    		std::cin>>score;
    	}
    
    	/*whatever else you are doing in your program. You can use
    	while loops like above for input verification.*/
    
    	return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. Replies: 18
    Last Post: 11-13-2006, 12:11 PM
  4. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:44 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 04:33 PM

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