Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Question about writing a program in visual c++
Closed Thread
Old 11-24-2004, 12:10 PM   #1 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default Question about writing a program in visual c++

Hi,
i have an assignment to write a program that does the following:
take an number & character from the user
and print it like this:
input : 7 & char: *
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
u get what i mean?
certainly i must use loops(while,for...?)but it has been a week now and i didn't get it done yet
any1 help plz??
DemonEdge is offline  
Old 11-25-2004, 02:39 AM   #2 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default

Any1 knows this plz???
DemonEdge is offline  
Old 11-25-2004, 02:48 AM   #3 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default

k...i tried writing it like following:

#include <iostream.h>
void main()
{
int num;
char #include <iostream.h>
void main()
{
int num;
char cha;
cout<<"Please enter a number: ";
cin>>num;
cout<<"Please enter a character: ";
cin>>cha;
for(int i=0;i<num;i++)
{
cout<<cha;
cha = cha + "" && + cha;
}
cout<<endl;
}

now the result is the following:

Please enter a number: 7
Please enter a character: *
*






Press any key to continue
DemonEdge is offline  
Old 11-25-2004, 04:27 AM   #4 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 750

slvrstang is on a distinguished road

Send a message via AIM to slvrstang
Default

try this

#include <iostream>
using namespace std;

int x;
int i;
int j;

void main(void)
{

cout << "Please input X";
cin >> x;
for(i=1;i<=x;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
i--;
for(;i>0;i--)
{
for(j=1;j<i;j++)
{
cout<<"*";
}
cout<<endl;
}
}


It defaults to * but you could change it

just add the char in the declarations and replace * with the char

like this

#include <iostream>
using namespace std;

int x;
int i;
int j;
char cha;

void main(void)
{

cout << "\nPlease input X: \n";
cin >> x;
cout << "\nPlease input a character: \n";
cin >> cha;
for(i=1;i<=x;i++)
{
for(j=1;j<=i;j++)
{
cout << cha;
}
cout<<endl;
}
i--;
for(;i>0;i--)
{
for(j=1;j<i;j++)
{
cout << cha;
}
cout<<endl;
}
}

Just make it a little prettier and your set
__________________

Q6600 G0 @ 3.80ghz
MCR-320-->Dtek Fuzion V2-->Dtek Fuzion GFX2-->Swiftech MCP-655-->
Asus P5K deluxe
4x1gb Crucial Ballistix @ 846mhz 5-5-5-15
8800GT
3x 250gb HDDs
Hiper 580w
Coolermaster 690 w/ water cooling mods
3dmark06 - 15,602
SuperPi 1M - 12.421s
slvrstang is offline  
Old 11-25-2004, 07:08 AM   #5 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default

YO MAN!!!
thx...i tried it and it works great
still...i need sometime to understand it.
thx again
DemonEdge is offline  
Old 11-25-2004, 07:13 AM   #6 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default

But 3 questions here really:
1)
what is the difference between "void main()" and ur "void main(void)"?
2)what is the difference between "#include <iostream.h>"
and "#include <iostream>" without h?
3)what does "using namespace std;" do?
DemonEdge is offline  
Old 11-25-2004, 08:08 AM   #7 (permalink)
 
Ultra Techie

Join Date: Jun 2004

Posts: 973

intercodes

Send a message via Yahoo to intercodes
Default

DemonEdge,

Quote:
1)
what is the difference between "void main()" and ur "void main(void)"?
There is no difference between those statements. Both take no arguments and return no value. In the second one I guess, 'void' is used explicitly to specify null arguments.

Quote:
2)what is the difference between "#include <iostream.h>"
and "#include <iostream>" without h?
AFAIK always the header files ends with ' .h ' . The new version of C uses only the header name without a h and the old ones' you need to specify the .h with the header in the' include' statment. [ There is some difference , I forgot. Its something to do with the path of the header file, i guess :rolleyes: ]

Quote:
3)what does "using namespace std;" do?
namespace are used to create collection of integers or floats or any other data types. In C++ header files, the data types are declared with the 'std' alias with the namespace.
So in older versions, you shld specify the functions as 'std.cin<<' , or 'std.cout>>' in the programs. Instead if you use 'using namepace std ' at the top of program , you dont need to define like 'std.cout<<' for the functions. You can just state as 'cout<<' .
For the new versions, you dont need to use the namespace anyz.


intercodes is offline  
Old 11-25-2004, 11:24 AM   #8 (permalink)
 
Newb Techie

Join Date: Nov 2004

Posts: 38

DemonEdge

Default

Quote:
Originally posted by intercodes

namespace are used to create collection of integers or floats or any other data types. In C++ header files, the data types are declared with the 'std' alias with the namespace.
So in older versions, you shld specify the functions as 'std.cin<<' , or 'std.cout>>' in the programs. Instead if you use 'using namepace std ' at the top of program , you dont need to define like 'std.cout<<' for the functions. You can just state as 'cout<<' .
For the new versions, you dont need to use the namespace anyz.
THX dudes for ur help,...
that means that the same code slvrstang wrote would function with or without "using namespace std:" when i am using a newer version of c++...right?
DemonEdge is offline  
Old 11-25-2004, 02:46 PM   #9 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 750

slvrstang is on a distinguished road

Send a message via AIM to slvrstang
Default

I always thought using namespace std had to do with the header files. Example

#include<iostream>
using namespace std

or

#include<iostream.h>

When using namespace std you don't need the .h at the end of the library.
__________________

Q6600 G0 @ 3.80ghz
MCR-320-->Dtek Fuzion V2-->Dtek Fuzion GFX2-->Swiftech MCP-655-->
Asus P5K deluxe
4x1gb Crucial Ballistix @ 846mhz 5-5-5-15
8800GT
3x 250gb HDDs
Hiper 580w
Coolermaster 690 w/ water cooling mods
3dmark06 - 15,602
SuperPi 1M - 12.421s
slvrstang is offline  
Old 11-25-2004, 02:48 PM   #10 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 750

slvrstang is on a distinguished road

Send a message via AIM to slvrstang
Default

Quote:
Originally posted by DemonEdge
YO MAN!!!
thx...i tried it and it works great
still...i need sometime to understand it.
thx again

Think of it as 2 nested for loops. 1 to make the ascending characters

*
**
***
****

and the largest
*****

and the second nested for loop for the descending characters

****
***
**
*

You'll figure it out, just a lot of abstract thinking
__________________

Q6600 G0 @ 3.80ghz
MCR-320-->Dtek Fuzion V2-->Dtek Fuzion GFX2-->Swiftech MCP-655-->
Asus P5K deluxe
4x1gb Crucial Ballistix @ 846mhz 5-5-5-15
8800GT
3x 250gb HDDs
Hiper 580w
Coolermaster 690 w/ water cooling mods
3dmark06 - 15,602
SuperPi 1M - 12.421s
slvrstang is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On