Like Tree3Likes
  • 2 Post By std10093
  • 1 Post By laserlight

problem in loop

This is a discussion on problem in loop within the C++ Programming forums, part of the General Programming Boards category; 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 ...

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

    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,675
    >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
    Salem and d4n1 like this.
    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
    21,451
    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?
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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, 03:06 PM
  2. Loop problem?
    By benrogers in forum C Programming
    Replies: 9
    Last Post: 02-07-2011, 11:26 PM
  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

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