I am extremely new to C++, can sum1 tell me a faster way to output stuff than cout? It either outputs too slow or takes too long to automatically scroll or something. I was trying to sort of output and refresh in real time w cout

Recommended Answers

All 23 Replies

Can you explain a bit more on what you are trying to do? Better if you can post the code. Otherwise there will be a lot of answers based on a lot of assumptions.

its really big, prolly outside the limits for posts. Im getting way ahead of myself, making a program this big wen i know nothing and probably would be able to make the same program better if i wait until i know more. but, basically its a program for the 15 slider puzzle and it gets confusing/disorienting or watever cause the info is flashing too fast for you to really make moves as fsast as you would like.

Wow, i need to read what I write before i post. If anyone wants, i can email it so you can see what i mean.

No need to email. You can attach your program here. Make sure you attach code, and not the executable.

Fancy. Ty. Its like 1008 lines and not well commented, though. Atleast it only uses simple stuff.

Skimmed through your program, and it is not cout that seems to be the problem. You are calling cout too much. Remember that optimization of code should start first with your algorithm. The rest will usually be taken care by a decent compiler. Take for example the for loops where you are outputting 50 newline characters.

for ( i = 0 ; i < 50; i++ )
   cout << '\n';

This piece of code calls cout 50 times. IO functions take a lot of time. That means you are writting to the console 50 times which takes a lot of time, when you consider the total number of times you do this in your program.
A better way will be to first create the string with 50 newline characters and output them all in a single cout call. like this

std::string newline( 50, '\n');
    std::cout << newline ;

Clean up your code like that, and you will get to know more modifications where you can make the code fast.

thanks. I don't know how to apply it, though.... Im new. I dont even know what std:: and that newline function do. (sry)

newline is not a function. It is a string object. What std::string newline( 50, '\n' ) does is, create a string called newline that has 50, '\n' characters. Since you have used using namespace std; at the beginning, you can just use string newline(50, '\n' ); . You will have to use the #include <string> at the beginning. Then cout just outputs it. I suggest you get a good C++ book if you dont have one, and start reading about C++ strings to learn more. you may fine good tutorials in the internet too.

Member Avatar for iamthwee

It's not a good idea to write a GUI in the console window.
You can do it of course, but you'll run into all sorts of problems.

If you have a java enabled browser this may give you some ideas as well.

Thanks again. Very much.

btw, have u tried compiling it?
I'm very proud of my primitive program ;)

Member Avatar for iamthwee

wats GUI? LOL

Graphical user interface. . .

So how can i write a GUI in C++ without the console window?

Thanks again. Very much.

btw, have u tried compiling it?
I'm very proud of my primitive program ;)

Well I compiled it, and ran it. Hate to say it but it sucks. There are too many user options to set, and too many controls to memorize. For example I saw the keys to move items only at the start. After the game starts I do not see it. Having a very short term memory, I could only remember that pressing w moves the item above the space down. So naturally I closed the game at that point.

But overall I think the effort is good. And I think you should be proud about your program too. Brush up the rusty patches, mentioned in my above posts. Concentrate on how to make the instructions more userfriendly. Don't go into GUI programming in C/C++ yet. Learn the language first. Otherwise you will fall in a lot of trouble.

int* fn0()
{int solution[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 7, 8,-1, 0, 0, 0, 0, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};return solution;}

Results in
$ g++ -W -Wall -ansi -pedantic -O2 15puzzle.cpp
15puzzle.cpp: In function ‘int* fn0()’:
15puzzle.cpp:278: warning: address of local variable ‘solution’ returned
For every function down to fnX()
When the function exits, the array no longer exists, so your pointer is invalid.
If it works for you, it is nothing more than luck - all I get is a segmentation fault.

Also, I get
15puzzle.cpp: At global scope:
15puzzle.cpp:723: warning: unused parameter ‘nNumberofArgs’
15puzzle.cpp:723: warning: unused parameter ‘pszArgs’
If you're not going to use the parameters, then just do
int main ( )

15puzzle.cpp: In function ‘void move(int, int*)’:
15puzzle.cpp:65: warning: ‘row’ may be used uninitialized in this function
15puzzle.cpp:65: warning: ‘col’ may be used uninitialized in this function
If the uninitialised value gets used, all sorts of chaos can happen.
Ensure the variables are initialised to something sensible before trying to use them.

Two more points
1. Using goto in that manner is a very poor substitute for a while loop.
2. A consistent approach to indentation will greatly assist you (and people who help you).

Thanks again. This is my slightly edited version, with string newline(59, '\n') . It is definitely faster. I didn't know what to do about the controls, though. All you need to know is that w, a, s and d move around. They're conveniently in the same shape as the arrow keys. I have no clue how to sort of getkey like basic, i can only input chars for now. And c at any time brings the controls back up. But this is my first non-trivial program. First one over maybe 50 lines that's more than a test of a feature.

Guess im lucky.:) I have the same warnings, but no actual compile errors. Again again thank you, I'm learning alot. I'm used to gotos from basic, i know theyre obsolete in C++. It was easier for me at the time, and really big loops are confusing to me. :confused: I will try.

Sry for my repeat posts, but i had a problem, and this was my solution. Like Salem said, it was lucky. I couldn't figure out how to store values to an array with {1,2,3....} unless it's being initialized. Obviously, the {1,2,3....} is the only practical way (that i know of) to store all the data in the arrays. I couldn't use conditions to only declare and initialize the array to a certain set of numbers, because even though it would only be declared once, the compiler errors and says im redeclaring as I have multiple declarations of the same variable in the same scope. So I made functions to do it and return a pointer to the array, so it wouldn't error because they all declare the array in a different scope. Not that it would matter if i gave 'solution' a different name in each function. It was faulty logic I know, but it seemed like an answer and it works. I also know now that the array is technically garbage data and goes out of scope when the function returns, and that any new variable could potentially mess with the array. I would like suggestions of better ways to store the info in the array. Also, the row & col should be initialised your right (just to 0 or something right?). I don't use them without storing something first, but what i store in them at first isn't constant.

> Guess im lucky. I have the same warnings, but no actual compile errors.
It is very difficult for newbies to tell which warnings are important, and which can be ignored. So you should really be fixing the ones you can fix, and asking questions about those you can't understand the reason for.

Even pros should be aiming for zero warnings as well.

> I would like suggestions of better ways to store the info in the array.
Voila

void getSolution ( int which, int *solution ) {
  static int solutions[][144] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 7, 8,-1, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0,
      0, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0,
      0, 9, 10,11,12,0, 0, 0, 0, 0, 0, 0,
      0, 13,14,15,-1,0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    /* Add more here */
  };
  int i;
  for ( i = 0 ; i < 144 ; i++ ) {
    solution[i] = solutions[which][i];
  }
}

In main, rather than having just a pointer, you would replace this

int current[144];
for (int i = 0; i < 144; i++)
current[i] = *(solution+i);

With this code, which would store the answer directly into the array you wanted it, without you having to do the copy in main (because the copy is now inside the called function).

case '0':
getSolution( 0, current );
break;
case '1':
getSolution( 1, current );
break;
case '2':
getSolution( 2, current );
break;

Perhaps prompt the user for a solution number rather than a solution letter, then you can remove the entire switch case with

int which;
cout << "Which one?";
cin >> which;
getSolution( which, current );

Thanks. I didnt think to use a matrix like that, so that i dont have to reference every solution individually, i can reference by number (which is sortof what i was looking for to begin with). I wouldn't need getSolution as a function, would I? Since it would only be called once in the program (or atleast only typed once)? Unless it has something to do with static? What does static do? Is it possible to return an array in a function, or only its pointer? And I still would like to know (even though i have no immediate need now) if its possible to store an array with {1,3,6,2....} while not initialising it. Tell me if i'm wrong, but i think i've noticed that you can use either an array or a pointer as an argument (they'd be used differently in the function, of course), and then you can pass either an array or a pointer and it will convert it according to the argument type.

I would like an answer...
I know they seem unrelated, but all these questions have come up from my 15 puzzle program (first program) and are probably simple to most people.

Thanks. I didnt think to use a matrix like that, so that i dont have to reference every solution individually, i can reference by number (which is sortof what i was looking for to begin with). I wouldn't need getSolution as a function, would I? Since it would only be called once in the program (or atleast only typed once)?

Use the function. It's modular and better programming practice.

Unless it has something to do with static?

Nope.

What does static do?

When used with a variable in a function, each time the function is entered the variable retains the value from the last time the function was used.

Is it possible to return an array in a function, or only its pointer?

Pointer...

And I still would like to know (even though i have no immediate need now) if its possible to store an array with {1,3,6,2....} while not initialising it.

Not a clue what you are asking. In order to get the values in the array it must be initialized somehow.

So how can i write a GUI in C++ without the console window?

To use GUI you should first learn Win32 API and/or MFC.
I suggest you learn C++ throughout in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.