Thread: About a loop program

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    About a loop program

    Hello,

    I have made a program of "for loop",
    I just want to take input from a user and if the numbers are float values, then the input should be accepted, also those input values should be entered in the "array" A[2];
    I want the user to enter a number in the input "cin" and I have some questions,


    Q.1 Should I keep the "cin" function before the for loop?


    Q.2 When I enter the wrong value in the program, I want the program to take me back to the input field where it prompts the user to input the data or number ,can you pls help me with this?


    Q.3 Have I placed the "break" statement at correct locations?


    Code:
    
    #include<iostream>
     
     using namespace std;
     
     
     
     main()
     
     {
     float x=2.5;
     float y=7.5;
     
     float A[2];
     float i;
      
      
     cout<<"pls enter a number"<< endl;
     
     
     cin>>i; 
      
      
      for (int i=0; i<8;i++)
      {
       
      
      
       if(i==x) 
       {
      cout<<"i="<<x<<endl;
      
        
      
      A[0]=i;
     }
     else
     cout<<"pls enter a number either 2.5 or 7.5"<<endl;
     
     if (i==y)
     
       {
        cout<<"i="<<y<<endl;
        
        
     
      A[1]=i;
     
      }
    else
     cout<<"pls enter a number either 2.5 or 7.5"<<endl; 
    break; 
    }
       
            
    system("pause");
    }
    Can you pls help me with this? Thanks
    Last edited by student111; 11-15-2013 at 02:55 PM. Reason: adding more

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by student111 View Post
    Can you pls help me with this? Thanks
    We can't help you if you don't try. Also, your code had horrible indentation, look here ( Indent style - Wikipedia, the free encyclopedia ) and choose a style to stick with. This sounds suspiciously like a homework question, and it sounds like you haven't made much effort to actually write the features you're asking us to do for you. You should come back once you have something that you need to actually fix, instead of asking us if it needs to be fixed.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    1) Not if you want to try to read input again on error.
    2) Check the result of the cin >> i call to determine if there is an error. There are also methods for getting a little information about the type of error.
    3) No, break is used to break out of the middle of the loop; it doesn't make sense to put it as the last line. Unless you wanted it inside that else condition, in which case you should fix that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. for loop program in c
    By panks in forum C Programming
    Replies: 5
    Last Post: 03-12-2011, 10:17 AM
  2. My program's in an infinite loop!
    By Peach in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 01:58 PM
  3. Need help with a loop program
    By hieugene in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2006, 02:42 AM
  4. how: loop program..
    By pico in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2004, 01:52 AM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM