3 input to print 3 outputs at the same time

This is a discussion on 3 input to print 3 outputs at the same time within the C++ Programming forums, part of the General Programming Boards category; I need this program to take three items for input then give three outputs for results. Is this program I ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Huintsville, AL.

    3 input to print 3 outputs at the same time

    I need this program to take three items for input then give three outputs for results. Is this program I wrote practical? its bug free and it builds but something in the style I dont like it seems I need some sort of nested loop in here not sure - and yes it's homework - but I just need advice and hints to improve it (that's if it needs to be improved)

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    	// initialization of Variables
    	string input1;
    	string input2;
    	string input3;
    	string veg [3];
    	string fruit [3];
    	string dairy [3];
    	veg [0] = "carrot";
    	veg [1] = "broccoli";
    	veg [2] = "tomato";
    	fruit [0] = "apple";
    	fruit [1] = "orange";
    	fruit [2] = "milk";
    	dairy [0] = "milk";
    	dairy [1] = "cheese";
    	dairy [2] = "kiwi";
    		//if statement
    		cout<<"Enter 3 Items"<<endl;
    		for(int i = 0;1 <3; i++)
    			if (input1 == veg [i])
    				cout<<input1<<"Is a Vegetable" <<endl;
    				if(input1 == dairy[i])
    					cout<<input1<<"Is a Dairy Product"<<endl;
    					if(input1 == fruit [i])
    						cout<<input1<<"Is a Fruit Product"<<endl;
    						if(input2 == veg [i])
    						cout<<input2<<"Is a Vegetable"<<endl;
    							if(input2 == dairy[i])
    								cout<<input2<<"Is a Dairy Product"<<endl;
    								if(input2 == fruit [i])
                                       cout<<input2<<"Is a Fruit Product"<<endl;
    									if(input3 == veg [i])
    										cout<<input3<<"Is a Vegetable"<<endl;
    										if(input3 == dairy [i])
    											cout<<input3<<"Is a Dairy Product"<<endl;
    										if(input3 == fruit [i])
    											cout<<input3<<"Is a Fruit Product"<<endl;
    			return 0;
    I hope i did the code blocks right

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Quote Originally Posted by jason007thomas View Post
    its bug free
    Never ever claim such a thing!
    I've already spotted a very obvious major bug in your for loop.

    Yes you got the code tags right, well done.

    There's no need for such deeply nested code. Use else-if like this:
    if (foo)
        cout << "foo";
    else if (bar)
        cout << "bar";
    else if (foobar)
        cout << "foobar";
        cout << "none of the above";
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    msh is offline
    Join Date
    Jul 2009
    Instead of doing this...
    string veg [3];
    veg [0] = "carrot";
    veg [1] = "broccoli";
    veg [2] = "tomato";
    Why not try something like this...
    string veg[3] = {"carrot", "broccoli", "tomato"};
    Check your for loop condition.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Why the need to create strings that contain "carrot" and compare to these when you could just compare to "carrot" directly?
    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.

  5. #5
    C lover
    Join Date
    Oct 2007
    for(int i = 0;1 <3; i++)
    You would likely want to change "1 < 3" to "i < 3". Otherwise, this would be infinite as 1 is always less than 3.

    for( int i = 0; i < 3; i++ );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NtSetSystemTime() failed
    By medp7060 in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2010, 02:58 AM
  2. Getting keyboard input and put into array and print
    By Rob123 in forum C Programming
    Replies: 0
    Last Post: 04-25-2009, 08:36 AM
  3. Recursive print every time...
    By BobDole11 in forum C Programming
    Replies: 5
    Last Post: 10-27-2008, 12:42 AM
  4. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 09:44 PM
  5. Military Time Functions
    By BB18 in forum C Programming
    Replies: 6
    Last Post: 10-10-2004, 01:57 PM

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