Thread: New to C++ help plz

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    20

    New to C++ help plz

    I want to get the out put like this .. if I input no 4 then for stars in a row and 4 colmns..

    ****
    ****
    ****
    ****
    if i input 10 then 10 stars 10 colomns

    I wrote the coding
    Code:
     #include "stdafx.h"
    #include <iostream>
    
    #include<limits>
    using namespace std;
    
    void main()
    { int x;
    cin >> x;
    for(int i=1;i<=x;i++)
    {
    	for(int g=1;g<=x;g++)
    	{
    		cout<<"*"<<endl;
    	}
    }
    system("pause");
    }
    butr this outputs like .. if I input 4 then the output comes as
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *

    how do I get this output??? whats wrong in my coding O.o

    ****
    ****
    ****
    ****

  2. #2
    Registered User godly 20's Avatar
    Join Date
    Jan 2011
    Location
    Italy
    Posts
    29
    You should use a multi-dimensional array.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Think about what std::endl does and particularly, what it does where you have placed it.

    Also, in C++ it is always int main, not void main.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    without using multi dimentional array how do you do it??

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    thanks
    I got it

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Quote Originally Posted by Shakti View Post
    Think about what std::endl does and particularly, what it does where you have placed it.

    Also, in C++ it is always int main, not void main.

    our teacher said its int mnain nah.. you sure.. ohh btw I placed the endl in the correct place
    Thanks

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    C++ standard explicitly says main shall return int unless for very specialized cases. If you are unsure wether you are using c++ in such a case then it is safe to say you aren't.

    Glad you got it

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    thanks oh and btw when you are using arrays in visual c++ 2008 what are the header files that we have to import?? and when we are using mod, factorial values n al what are the header files that you have to import in visual c++ 2008

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    for arrays you dont need to include anything. For std::vector though you need to include vector, for math-functions you need cmath. Mod however is a built in operator
    a % b is a mod b.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Quote Originally Posted by Shakti View Post
    for arrays you dont need to include anything. For std::vector though you need to include vector, for math-functions you need cmath. Mod however is a built in operator
    a % b is a mod b.

    but then why is my code not working?
    Code:
    #include "stdafx.h"
    #include <iostream>
    
    #include<limits>
    using namespace std;
     void main()
    { 
    	int array[20];
    	int tot=0;
    	for(int i=0;i<19;i++)
    	{
    		cin<<array[i];
    		tot=tot+array[i];
    	}
    	float avg=tot/20;
    	cout<<"tot"<<"="<<tot<<   <<"avg"<<"="<<avg;
    	system("pause");
    
     }
    Error 1 error C2784: 'std::basic_ostream<char,_Traits> &std:perator <<(std::basic_ostream<char,_Traits> &,unsigned char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'std::istream'

    Error 2 error C2784: 'std::basic_ostream<char,_Traits> &std:perator <<(std::basic_ostream<char,_Traits> &,unsigned char)' : could not deduce template argument for 'std::basic_ostream<char,_Traits> &' from 'std::istream'

    Warning 42 warning C4244: 'initializing' : conversion from 'int' to 'float', possible loss of data c:\documents and settings\administrator\my documents\visual studio 2008\projects\oogh\oogh.cpp 16 oogh

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't have << and then << again with nothing in between them. If you want to print some spaces, then you need to print a string with spaces in it, i.e., " ".

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by godly 20
    You should use a multi-dimensional array.
    No, you should not use a multi-dimensional array. In fact, there is no need for an array or a container like std::vector at all. Sure, you could use them (though using std::string would actually make it easier), but they are unnecessary.

    Your original idea is correct. The problem is that you printed a new line (through endl) in the wrong place.
    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

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Quote Originally Posted by tabstop View Post
    You can't have << and then << again with nothing in between them. If you want to print some spaces, then you need to print a string with spaces in it, i.e., " ".
    still the errors comes nah

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Quote Originally Posted by laserlight View Post
    No, you should not use a multi-dimensional array. In fact, there is no need for an array or a container like std::vector at all. Sure, you could use them (though using std::string would actually make it easier), but they are unnecessary.

    Your original idea is correct. The problem is that you printed a new line (through endl) in the wrong place.


    No i didnt print a new line.., this is my code
    Code:
    
    #include "stdafx.h"
    #include <iostream>
    
    #include<limits>
    using namespace std;
     void main()
    { 
    	int array[20];
    	int tot=0;
    	for(int i=0;i<19;i++)
    	{
    		cin<<array[i];
    		tot=tot+array[i];
    	}
    	float avg=tot/20;
    	cout<<"tot"<<"="<<tot<<"avg"<<"="<<avg;
    	system("pause");
    
     }

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by thili
    No i didnt print a new line.., this is my code
    I am talking about the code that you posted in post #1. However, I now notice that you already solved it as per post #5. You should have started a new thread concerning your new code and new problem.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. project .... help plz plz plz
    By Hitchhik3R in forum C Programming
    Replies: 13
    Last Post: 10-24-2010, 02:07 PM
  2. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM