Thread: problem in loop

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    problem in loop

    Code:
    #include<iostream.h>
    #include<conio.h>
    class item{
    public:
    char name[20];
    int price[10];
    void enter();
    };
    void item::enter()
    {
    for(int i=0;i<5;i++){
    cout<<"\nEnter name of Item "<<(i+1);
    cin>>name[i];
    cout<<"\nEnter the price in Rs : ";
    cin>>price[i];
    }
    }
    void main()
    {
    clrscr();
    int a;
    item chips;
    chips.enter();
    cout<<"/nEnter the max price : ";
    cin>>a;
    for(int j=0;j<5;j++){
    if(chips.price[j]<=a)
    cout<<"/n"<<chips.name[j];
    }
    getch();
    }

    the loop does not work correctly
    please help!

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    >the loop does not work correctly
    So does your indentation!

    Also, mind that conio.h is not in the standard (!!!). I would sure not use it if I were you

    We also write
    Code:
    int main( )
    {
           ....
           return 0;
    }
    instead of main void
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your code properly.

    This is pre-standard:
    Code:
    #include<iostream.h>
    It should be:
    Code:
    #include <iostream>
    Oh, and then you will need to qualify names, e.g., write std::cout instead of just cout, or say, place the using directive using namespace std; after the header inclusions.

    This is non-standard, which is sometimes fine:
    Code:
    #include<conio.h>
    but you are only using it for getch, which you don't need as you can say, run the program in a command prompt window or use the standard cin.get() instead. Okay, you are also using it from clrscr, but frankly you don't need to do so.

    Your item class enter member function should not be a member function at all. Furthermore, perhaps if you changed it to take an argument, you would be able to reason about it better. At the moment, you seem to be confused as to whether it works on a single item or many items.

    This is wrong:
    Code:
    void main()
    It should be:
    Code:
    int main()
    Quote Originally Posted by royroy
    the loop does not work correctly
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    12
    Code:
    #include<iostream>
    #include<conio.h>
    
    
    using namespace std;
    class item{
    public:
    char name[20];
    int price;
    void enter(int);
    };
    void item::enter(int i)
    {
    cout<<endl<<"Enter name of Item "<<i<<":";
    cin>>name;
    cout<<endl<<"Enter the price in Rs : ";
    cin>>price;
    }
    
    
    int main()
    {
    system("cls");
    int a;
    item chips[5];
    for(int i=0;i<5;i++)
    chips[i].enter(i+1);
    cout<<endl<<"Enter the max price : ";
    cin>>a;
    for(int j=0;j<5;j++){
    if(chips[j].price<=a)
    cout<<endl<<chips[j].name;
    }
    getch();
    return 0;
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should still indent your code.
    You should replace your char array name with std::string. Clearly you are expecting an array of strings and not an array of single characters.
    Be sure to take a look at SourceForge.net: Safer arrays in Cpp - cpwiki to see what C++ has to offer in terms of arrays and how dangerous they can be.
    Here is some good advice: SourceForge.net: Do not remove parameter names - cpwiki
    Also know that system is a potential security risk. You really should avoid it. There is no need to clear the screen.
    Finally, consider making your member variables private and add appropriate getter functions to access them.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Be sure to take a look at SourceForge.net: Safer arrays in Cpp - cpwiki to see what C++ has to offer in terms of arrays and how dangerous they can be.
    container.size() is still conspicuously missing as a solution.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have a point. I will add a section about legacy interfaces with containers.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a for loop
    By TobiasK in forum C Programming
    Replies: 5
    Last Post: 02-05-2013, 04:06 PM
  2. Loop problem?
    By benrogers in forum C Programming
    Replies: 9
    Last Post: 02-08-2011, 12:26 AM
  3. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  4. Do While Loop Problem
    By DrKillPatient in forum C++ Programming
    Replies: 8
    Last Post: 10-13-2005, 09:58 AM
  5. Problem with while loop
    By Fyodorox in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2002, 11:02 PM