Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 05-23-2005, 08:34 AM   #1 (permalink)
Soundmaster2.0's Avatar
 
True Techie

Join Date: Mar 2005

Posts: 184

Soundmaster2.0 is on a distinguished road

Default C++ output character

What is the function used to output a certain character a certain number of times?
__________________

Aim:Gibsonrocker03
Soundmaster2.0 is offline  
Old 05-24-2005, 08:41 AM   #2 (permalink)
 
Super Techie

Join Date: May 2005

Posts: 479

furtivefelon

Default

try a loop?
__________________
lisp hacker
running: FreeBSD 5.4 - still learning
develop with: SBCL + emacs for lisp, Anjuta IDE +gcc for c, SPE for python..
browse with: opera
furtivefelon is offline  
Old 05-25-2005, 03:33 AM   #3 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

1) Copy the character in an array - memset(void *str, int c, size_t n)
2) printf
Chankama is offline  
Old 05-25-2005, 08:56 AM   #4 (permalink)
Soundmaster2.0's Avatar
 
True Techie

Join Date: Mar 2005

Posts: 184

Soundmaster2.0 is on a distinguished road

Default

nvm i found a faster less annoying way
__________________

Aim:Gibsonrocker03
Soundmaster2.0 is offline  
Old 05-25-2005, 02:10 PM   #5 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

So care to tell us what is faster :eek: and less annoying :eek: than a single line
printf("...", memset(...) ) ?
Chankama is offline  
Old 05-25-2005, 06:09 PM   #6 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

printf("...", memset(...) ) that is in C not C++.

char theCh = '#';

for(int i = 0; i<9; ++i)
{
cout<<theCh<<endl;
}
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 05-25-2005, 08:28 PM   #7 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

Well the C commands works in C++. You can use <stdio.h> or <iostream> or whatever.

In anycase, using the cout << operator or the cout.put or printf function to output each character separetely in a LOOP is a terrible way of doing this. You are essentially calling a function each time to output one character. Not very robust at all. It's slow and looks ugly. This type of coding is good for a high school project, but that's about it..

So to output 1000 characters, u want to call the function 1000 times?? :eek:

If you want to use cout instead of printf,
cout << memset(...);

Still one line and only 2 function/operator calls ..
Chankama is offline  
Old 05-27-2005, 01:38 AM   #8 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

Did i call it 1000 times, no i don't think so. If he asked me to call it 1000 times i would have done it differently. Did he say it must be robust and efficient no. The question he asked was very simple so i assumed he is doing a simple project. So he would only have been taught a certain simple way. If i was a teacher and i saw a student that does not know how to ouput a character 7 or so times do what you did, i would suspect plagarism. Not all C++ students understand C.

Now if you want to convey an idea across to a student would you explain a clear simple way, or a way that offers no clarity? I think the best method is to explain how to do it easily(something you can see work) first and then show a more efficient method later.

Everyone knows that C code works in C++, C++ came from C it's kind of obvious. All programmers should know how to use memset anyway because it is very cool and works very efficiently.

Code:
  
#include <iostream>
using namespace std;

int main()
{
	char str[1000]= "";
	memset(str, '-', 1000);
	puts(str);
	
	return 0;
}

__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n is offline  
Old 05-27-2005, 03:07 AM   #9 (permalink)
Chankama's Avatar
 
Monster Techie

Join Date: Jan 2005

Location: Canada

Posts: 1,522

Chankama will become famous soon enough

Default

Yeah.. I think it's always nice when students start off learning C.. Even before Java and such. It's unfortunate that a lot of teachers don't stress the value of C. I mean, just to write a simple program in Java, you need to teach them objects, member functions, etc. etc.
Chankama is offline  
Old 05-28-2005, 02:58 AM   #10 (permalink)
 
Super Techie

Join Date: Jan 2005

Posts: 295

gab00n

Default

Yeah all the universities jumped on the Java and C++ band wagon and now they are realizing their mistake. My professors say that if they could choose they would go back to C and would also teach Fortran, i think they will soon. Every engineer should still learn Fortran and Cobol because there is a ton of code out there that business's don't want to replace. Plus there are some cool stuff that you can teach in C that isn't in C++.
__________________
\"Today\'s scientists have substituted mathematics for experiments, and they wander off through equation after equation, and eventually build a structure which has no relation to reality.\" Nikola Tesla
gab00n 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