[HELP] C++ Programming... ERROR

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post please read how to get help
Gugi
Level 1
Level 1
Posts: 32
Joined: Sat Sep 29, 2018 11:45 am
Location: United States
Contact:

[HELP] C++ Programming... ERROR

Post by Gugi » Sat May 04, 2019 5:15 am

Hello, today i finally decide with which program language i will code.
After few hours i finally get my first ERROR, and i want you to help me find way out :)

So at first point, my code have to count from 15 to 0 and after this says " Hell Yeah"

and from point where is no ERRORS it looks like:

Code: Select all

#include <iostream>
#include <unistd.h>
#include <cstdlib>

using namespace std;

int main()

        {

        for (int i=15; i>=0; i--)
    {


            cout << i <<endl;
    }
        cout<<"Hell Yeah!";

        return 0;
        }

But when i want add

Code: Select all

sleep(1000);

        system("clear");
Program should count from 15 to 0 and says Hell Yeah, but here i found my FIRST ERROR :D

Code: Select all

#include <iostream>
#include <unistd.h>
#include <cstdlib>

using namespace std;

int main()

        {

        for (int i=15; i>=0; i--)
    {
      sleep(1000);

        system("clear");

            cout << i <<endl;
    }
        cout<<"Hell Yeah!";

        return 0;
        }
In my Console it looks like:
Image
https://imgur.com/YfSBX2K

Any ideas why?

I am using Code:Blocks IDE and GNU GCC compiler, as TUTORIAL says.
Last edited by karlchen on Sat May 04, 2019 5:29 am, edited 1 time in total.
Reason: added screenshot preview via [rimg] tags
Image

REGARDS,
Gugi

jchelpau
Level 3
Level 3
Posts: 100
Joined: Mon Mar 25, 2019 11:19 pm
Location: Australia
Contact:

Re: [HELP] C++ Programming... ERROR

Post by jchelpau » Sat May 04, 2019 5:49 am

You're sleeping for 1000 seconds.

sgtor
Level 4
Level 4
Posts: 312
Joined: Sat May 13, 2017 9:39 pm

Re: [HELP] C++ Programming... ERROR

Post by sgtor » Sat May 04, 2019 5:55 am

If you really want to learn c++ you're better off trying to figure it out yourself. I'll tell you anyway though.

I'm pretty sure you need ; after the i-- in your for loop.

Code: Select all

i--;
It's been quite a few years since I programmed in c++ though so I could be mis-remembering the proper syntax.

rene
Level 10
Level 10
Posts: 3307
Joined: Sun Mar 27, 2016 6:58 pm

Re: [HELP] C++ Programming... ERROR

Post by rene » Sat May 04, 2019 7:56 am

sgtor wrote:
Sat May 04, 2019 5:55 am
I'm pretty sure you need ; after the i-- in your for loop.
I'm sure he doesn't (see jchelpau instead) but what's with the indentation nightmare? I assumed I was looking at a spaces/tab copy-paste issue until the screenshot...

Hoser Rob
Level 14
Level 14
Posts: 5275
Joined: Sat Dec 15, 2012 8:57 am

Re: [HELP] C++ Programming... ERROR

Post by Hoser Rob » Sat May 04, 2019 9:46 am

Gugi wrote:
Sat May 04, 2019 5:15 am
Hello, today i finally decide with which program language i will code....
There are few prorgaming languages less suited for beginner progrmamers than C/C##. Just wait until you get to pointers.

Seriously, do yourself a favour and start with Python or perl.

User avatar
Flemur
Level 16
Level 16
Posts: 6933
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: [HELP] C++ Programming... ERROR

Post by Flemur » Sat May 04, 2019 10:31 am

This works (note: sleep(1) ):
call the file "confused.cc", then g++ confused.cc , then ./a.out and it counts down from 15 with clear terminal and the single number in the upper left corner, and ends on:

Code: Select all

0
Hell Yeah!$ 

Code: Select all

#include <iostream>
#include <unistd.h>
#include <cstdlib>

using namespace std;

int main()
{

    for (int i=15; i>=0; i--)
    {
     	sleep(1);

	system("clear");

	cout << i <<endl;
    }
    cout<<"Hell Yeah!";

return 0;
}
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 19.1 Xfce/fluxbox
Manjaro openbox/fluxbox

gm10
Level 17
Level 17
Posts: 7449
Joined: Thu Jun 21, 2018 5:11 pm

Re: [HELP] C++ Programming... ERROR

Post by gm10 » Sat May 04, 2019 10:43 am

Hoser Rob wrote:
Sat May 04, 2019 9:46 am
Gugi wrote:
Sat May 04, 2019 5:15 am
Hello, today i finally decide with which program language i will code....
There are few prorgaming languages less suited for beginner progrmamers than C/C##. Just wait until you get to pointers.

Seriously, do yourself a favour and start with Python or perl.
I agree it's harder to start with C++, but I also think it's better because you (necessarily) gain a better understanding about various aspects of the system and programming it and I would hope that would lead to better code. You'd probably still have many of the same difficulties with C++ if you were to learn Python first, but then you'd also have to overcome bad habits you learned from Python that won't help you with C++.

What he needs is a tutorial written for Linux though - clearly he's using one for Windows where sleep() takes milliseconds as a parameter. And a better IDE apparently, what's up with that horrible indentation?

User avatar
Flemur
Level 16
Level 16
Posts: 6933
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: [HELP] C++ Programming... ERROR

Post by Flemur » Sat May 04, 2019 10:55 am

gm10 wrote:
Sat May 04, 2019 10:43 am
And a better IDE apparently, what's up with that horrible indentation?
code::blocks
I agree there's no point in discouraging the guy from writing c++ code.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 19.1 Xfce/fluxbox
Manjaro openbox/fluxbox

jchelpau
Level 3
Level 3
Posts: 100
Joined: Mon Mar 25, 2019 11:19 pm
Location: Australia
Contact:

Re: [HELP] C++ Programming... ERROR

Post by jchelpau » Sat May 04, 2019 11:31 am

If you can deal with it, learning C or C++ is a good skill and allows you to patch a wide variety of software.

Gugi
Level 1
Level 1
Posts: 32
Joined: Sat Sep 29, 2018 11:45 am
Location: United States
Contact:

Re: [HELP] C++ Programming... ERROR

Post by Gugi » Sat May 04, 2019 12:05 pm

Thanks guys.

I will learn C++, i dont care how hard it is.

I solved this with sleep(1); and it works

Idk why i cant even do system("clear"); but nevermind

Yes, i am working with many independencies and it looks horrible, but i can show you up something :D
but... IT DOESNT MATTER how hard it will be for me :)

Image
Image

REGARDS,
Gugi

User avatar
AndyMH
Level 8
Level 8
Posts: 2158
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: [HELP] C++ Programming... ERROR

Post by AndyMH » Sat May 04, 2019 12:12 pm

If it took a few hours before your first error you're doing well. I use c++ for arduinos, for other stuff in linux I use lazarus (pascal). Think it's easier than c++ and a great IDE.
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

sgtor
Level 4
Level 4
Posts: 312
Joined: Sat May 13, 2017 9:39 pm

Re: [HELP] C++ Programming... ERROR

Post by sgtor » Sat May 04, 2019 12:17 pm

rene wrote:
Sat May 04, 2019 7:56 am
sgtor wrote:
Sat May 04, 2019 5:55 am
I'm pretty sure you need ; after the i-- in your for loop.
I'm sure he doesn't (see jchelpau instead) but what's with the indentation nightmare? I assumed I was looking at a spaces/tab copy-paste issue until the screenshot...
Hence why I said I could be mis-remembering the syntax.

User avatar
AndyMH
Level 8
Level 8
Posts: 2158
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: [HELP] C++ Programming... ERROR

Post by AndyMH » Sat May 04, 2019 12:50 pm

but what's with the indentation nightmare
Agreed. Mine would have looked like:

Code: Select all

#include <iostream>
#include <unistd.h>
#include <cstdlib>

using namespace std;
/*------------------------------------------------------
 * Main
   What does it do?
 --------------------------------------------------------*/
int main() {
  for (int i=15; i>=0; i--) {
       sleep(1);
       system("clear");
       cout << i <<endl;
       } //end for
  cout<<"Hell Yeah!";
  return 0;
}//end main
But each to their own. Biggest 'crime' is the total lack of comments - learn now to comment your code as you go. Otherwise how will you understand what you were trying to do in six months time?
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

User avatar
thx-1138
Level 7
Level 7
Posts: 1803
Joined: Fri Mar 10, 2017 12:15 pm
Location: Athens, Greece

Re: [HELP] C++ Programming... ERROR

Post by thx-1138 » Sat May 04, 2019 12:57 pm

...Yet another thread with a very specific question,
totally side-tracked to a generalized discussion in regards to programming, exactly due to...
Poser Rob's desperate attempts to somehow appear relevant & knowledgeable.
Since he couldn't answer such for the life of his, he'd better go off to copy/paste some...easylinuxtips
(or tell us for the 1000th time how much he agrees with Pjotr, again, in order for him to appear relevant) - by hey, whatever...

Gugi, in a few days, as a fun project / exercise,
you could possibly come up a bingo card generator with his past statements in those forums:
I wouldn't recommend C or C++ to start ...
they are NOT high level programming languages (more like high level assembly language) and not meant for beginners.
Try Python instead. It's a good starting language and I read recently (in IEEE Spectrum I think) it's the #1 used language at the moment.
I'd try Python nif you haven't done it for a bit. C/C++ is NOT a good one to start with,
it's not a high level language and if you don't understand pointers and recursion forget C.
G++ is just short for GNU C++. If you're this new to programming, C++ is one of the worst choices for a language to start with.
It's like trying to learn how to drive in a Lamborghini with no traction control in a blizzard. Familiar with recursion and pointers?
C is NOT a good language to start programming with. It isn't a high level lnguage, more like a high level assembler.
I cannot think of a WORSE way for a newbie to start learning programming than a kernel building project using C and C++ like this.
Seriously, this is not meant for beginners ...
most university CS programs don't introduce C programming until the 2nd year ... and dont't expect any guru support.
Xlib is written in C or C++. There are few worse languages for learning programming on.
C is not a high level programming language, it's more like a high level assembler. It assumes knowledge begiinners don't have.
WAIT - it does get way way better as well:
There are many C posers out there. Ask them to explain pointers.
That'll separate the men from the boys. You can't do anything in C without pointers.
However, many newbies think there's such a thing as "linux progamming". There isn't. Programming is actually platform independent.
I know how silly that sounds ... I admit you do need a platform to test your programs ...
but the principles underlying it are the same on any platform.
Frankly I'd be surprised if any of those devs used Pascal ...
it's one of those programming languages that are often taught in 1st year CS courses but rarely used in the "real world".
(Reply he earned himself): 'Huh, do you know "Skype" or "Photoshop" ?Those applications were developed in Pascal.'
Q:What's the programming language Linux Mint has been coded in?
A: Python, JavaScript and C mainly.
(Poser Rob's 'additional' answer): I would not think Python nor JavaScript as these two languages are not compiled.
The last time I ever installed by building and compiling a tar.gz source was a backported wireless driver, years ago.
Won't touch them unless absolutely necessary.

rene
Level 10
Level 10
Posts: 3307
Joined: Sun Mar 27, 2016 6:58 pm

Re: [HELP] C++ Programming... ERROR

Post by rene » Sat May 04, 2019 1:49 pm

thx-1138 wrote:
Sat May 04, 2019 12:57 pm
Yet another thread with [ ... ]
Wow, dude... :-)
AndyMH wrote:
Sat May 04, 2019 12:50 pm
Agreed. Mine would have looked like:
Here's my entry, with which I (also) hope to make some specific comments regarding learning C++.

Code: Select all

#include <iostream>
#include <chrono>
#include <thread>

int main()
{
	for (int i = 15; i >= 0; i--) {
		std::this_thread::sleep_for(std::chrono::seconds(1));
		std::cout << "\ec" << i << std::endl;
	}
	std::cout << "Hell Yeah!" << std::endl;
	return 0;
}
Stylewise I specifically note the "functions have opening brace on the next line, anything else on the same" difference with Andy's but that's undoubtely more since I would feel myself more familiar with plain C rather than C++ and I do believe Andy's preference is in fact more ++ish. It's also of course not important.

First of two points I do want to make with this is that OP's version IS more or less C rather than C++ through his use of sleep() and, to lesser degree, system(). Since C++11 a more native C++ sleep_for() is provided as per above. Since its use also means using std::this_thread and std::chrono this also looses the "using namespace std"; it's generally advised to be explicit about namespaces, although, yes, no one is going to hold it against you if you get tired of typing std:: over and over again generally.

But that's the first point then: when writing C++ do try to write C++ rather than C.

Second this looses the system() altogether. In either C or C++ using system() more or less --- sometimes more, sometimes less --- amounts to "cheating". You are launching an entirely different executable to do something you could just do yourself. On Linux you clear a terminal through an escape sequence, specifically ESC-c, as you see being outed directly above.

Or better, "usually ESC-c", since what to in fact output is a specific terminal property, which the by yourself used program clear gets from the terminfo database. That is, if you'd want to be really "correct" you'd look into obtaining the actual sequence from terminfo, in turn through curses or directly; doing that might be nice practice -- but would again be more C-in-C++ since as far as I'm aware no specific C++ terminfo support is available; the above just outs \ec and calls it a day.

But that's the second point then: when writing in a lower level language such as C or C++ do try to write C or C++ rather than shell :)

sgtor
Level 4
Level 4
Posts: 312
Joined: Sat May 13, 2017 9:39 pm

Re: [HELP] C++ Programming... ERROR

Post by sgtor » Sat May 04, 2019 9:36 pm

Gugi wrote:
Sat May 04, 2019 12:05 pm
Thanks guys.
I will learn C++, i dont care how hard it is.
I applaud you for that!

I do still stand by what I said about trying to figure it out yourself though, you'll learn way more in the end and it will also teach you one of the skills you need as a programmer. That is the skill to figure out why your code is buggy or throwing errors. So how do you figure it out? Read the reference documentation. In this case it would have been the manual for sleep(). At first glance I didn't see the problem as being sleep() but if I was trying to debug that code I would have figured it out by reading the manual and commenting out sleep to see if that was indeed causing the error.

Code: Select all

//sleep(1000);


If you start doing this, commenting out the errors where you think they are then you can narrow down exactly what the problem is and start figuring out how to fix it. Using this method you probably wouldn't have needed to ask your question I think you would have found the answer on your own. That being said feel free to ask questions if you need to but you will learn way more if you don't.

And I know my for loop comment was wrong but I knew it could be wrong before I posted the answer so don't dismiss my advise based on that comment. It's been years since I programmed in c++ and when I did I almost never used for loops, I always used while loops.

And I actually do agree with rene that your code could be seen as more C rather than C++. You're not using OOP. And why do I bring up this point? As you're learning you'll probably be using more of a C structure than a C++ structure unless your tutorial jumps right into OOP. So yes learn the C structure because it is the proper way to code(as opposed to spagetti code) but, and this is important, don't become to rigid in your mind with it because OOP is a whole different animal. What I mean is learn it but don't accept it as the only way to program, remember that you still need to learn OOP and it's totally different.

jchelpau
Level 3
Level 3
Posts: 100
Joined: Mon Mar 25, 2019 11:19 pm
Location: Australia
Contact:

Re: [HELP] C++ Programming... ERROR

Post by jchelpau » Sat May 04, 2019 11:29 pm

sgtor wrote:
Sat May 04, 2019 9:36 pm
I do still stand by what I said about trying to figure it out yourself though, you'll learn way more in the end and it will also teach you one of the skills you need as a programmer. That is the skill to figure out why your code is buggy or throwing errors. So how do you figure it out? Read the reference documentation. In this case it would have been the manual for sleep(). At first glance I didn't see the problem as being sleep() but if I was trying to debug that code I would have figured it out by reading the manual and commenting out sleep to see if that was indeed causing the error.

Code: Select all

//sleep(1000);


If you start doing this, commenting out the errors where you think they are then you can narrow down exactly what the problem is and start figuring out how to fix it. Using this method you probably wouldn't have needed to ask your question I think you would have found the answer on your own. That being said feel free to ask questions if you need to but you will learn way more if you don't.
This is exactly what I did to find out that sleep wasn't working correctly. Then I read the manual for the sleep function to see it used seconds.
Debugging is a very, very important skill to develop when writing any kind of programming code.

User avatar
AndyMH
Level 8
Level 8
Posts: 2158
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: [HELP] C++ Programming... ERROR

Post by AndyMH » Sun May 05, 2019 3:58 am

You're not using OOP. ... because OOP is a whole different animal.
This is very important. And it is a mindset thing. If you want to learn C++ then you must start learning what classes, objects and methods are and use them in your code from day one. I struggle here because I'm of an era where SSADM was the way to program - that gives me a linear mindset which is not what OOP is. In OOP your program would look more like (pseudo code looking like pascal rather than C++):

Code: Select all

type
  myObject = class
public
  //any procedures, functions, variables that can be used externally with myObject
  constructor Create;
  procedure Countdown;
private
  //any procedures, functions, variables not visible to the outside world
end;

procedure myObject.Countdown(var mytime : integer);
var
  i : integer; //countdown timer
begin
  for i:= 10 downto 1 do delay(mytime);
  writeln('hello world');
end;

begin //main program
  myObject.Create; //creates an instance of myObject
  myObject.Countdown(1000); //this is a method of myObject
  myObject.Free; //destroys myObject and frees up the memory it was using
end.
It is a completely different way of thinking, and the sooner you start with this, the easier it will be. If your end goal is to write GUI applications then this is what you need to do. GUI = event driven = OOP.
Homebrew i5-8400+GTX1080 Cinnamon 19.0, 3 x Thinkpad T430 Cinnamon 19.0, i7-3632 , i5-3320, i5-3210, Thinkpad T60 19.0 Mate

Gugi
Level 1
Level 1
Posts: 32
Joined: Sat Sep 29, 2018 11:45 am
Location: United States
Contact:

Re: [HELP] C++ Programming... ERROR

Post by Gugi » Sun May 05, 2019 5:35 am

sgtor wrote:
Sat May 04, 2019 9:36 pm
Yes i am using

Code: Select all

//comment

Thanks for advice :D
Image

REGARDS,
Gugi

Hoser Rob
Level 14
Level 14
Posts: 5275
Joined: Sat Dec 15, 2012 8:57 am

Re: [HELP] C++ Programming... ERROR

Post by Hoser Rob » Wed May 08, 2019 10:31 am

The level of mystification regarding OOP is ridiculous. The purpose of it is to make programs more maintainable. That's what it's for, and it's done by embedding basic operations for a data type within the data structure itself. If you don't understand data structures you aren't ready fo9r C o9r C++. And C++ is a bad place to learn OOP, as it';s a bad place to learn programming in general.

Post Reply

Return to “Other topics”