Jump to content

Whats wrong with my c++ code

Evellence

Hello!

 

So i got a homework to do like week ago, its about c++ code. I've been trying and mashing my head since then. It have 3 tasks. 

My friend helped me, and we kinda finished. But professor told me that all 3 are not working. I don't really know  much about programming, and that friend of mine has gone on vacation.

 

So i will post all 3 assignments here, please look at it and tell me whats wrong so i can fix it if i can.

 

Task 1

Spoiler

Write a program in the C ++ programming language to calculate the absolute value of a complex number with the definition of a class and forming an object.
The program should be done as follows:
• declare the class KomplBroj that calculates the absolute value of the complex number;
• within the class KomplBroj use the variable (attribute) m which is a decimal number (float) and is valid only within the class (private);
• Within the class a function that is visible outside of the class itself (public) according to the form m = sgrt (a * a + b * b) is defined within the class;

• the object inherits the properties of the class and is used in the main program;
• in the main program, the object is defined by specifying the class name and object name in the following way: KomplBroj Z;
The same task is solved by calling subroutine (function) by structural programming.
Solve the same task with classical programming.

My solution

Spoiler

#include <iostream>     /*std::cout, std::end */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* abs */
#include <math.h>       /* sqrt */
#include <fstream>
#include <string>
#include <iomanip>

int main () {

  int a=100;
  double b=3.14;

  std::cout << a;
  std::cout << std::endl;              
  std::cout << b << std::endl << a*b;  
  std::endl (std::cout);               

  return 0;
}


int main ()
{
  int n,m;
  n=abs(23);
  m=abs(-11);
  printf ("n=%d\n",n);
  printf ("m=%d\n",m);
  return 0;
 }
class KomplBr		//Deklarisanje klase
{
private:Sasa Petrovic
float m;

public:
float moduo(float a, float b)	//Definisanje funkcije
int main ()
{
  double param, result;
  param = 1024.0;
  result = sqrt (param);
  printf ("sqrt(%a) = %a\b", param, result );
  return 0;
}
};
using namespace std;
int main( int argc, char* argv[] )
  {
  cout << " Zadaj realni i imaginarni deo kompleksnog broja: " << argv[ 0 ]
       << "\nArguments are:\n";
  for (int n = 1; n < argc; n++)
    cout << setw( 2 ) << m << ": " << argv[ m ] << '\m';
  return 0;
  }
  if (!infile)
{
cout<<"Cannot open input file "<< endl;
system("pause");
return 1;
}
/* abs Output:n=23 m=11 */
/*Output:sqrt(1024.000000) = 32.000000*/
/*Output:std:endl100 3.14 314*/
 

 

 

 

 

 

Task 2

Spoiler

Write the program in the C ++ programming language to calculate the square of the square with the definition of a class and the formation of an object.
The program should be done as follows:
• declare the Kvadar class that calculates the square of the square;

• In the Kvadar class, use a variable (attribute) pov that has a decimal number (float) and applies only within the class (private);

• Within the class, the Kv function is defined, which is visible outside the class itself (public) according to the form pov = 2 * (a * b + b * c + c * a);

• the object inherits the properties of the class and is used in the main program;
• In the main program, the object is defined by specifying the class name and object name in the following way: Kvadar P.
The same task is solved by calling subroutine (function) by structural programming.
Solve the same task with classical programming.

My solution

Spoiler

#include <stdio.h>      /* printf */
#include <math.h>       /* pow */
#include <cstdlib>
#include <iostream>
#include <cmath>

class Kvadar //		//Deklarisanje klase
{
private: Sasa Petrovic


using namespace std;

int main(int argc, char *argv[ ])
{
float a, b, c;
Kvadar P; // napravi objekat p koji je klase Kvadar
cout << "Zadaj ivice kvadra: "<<endl;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
cout << "c=";
cin >> c;
//cin >> a >> b >> c;
cout << "Povrsina kvadra je " << P.Kv(a, b, c) <<endl;


int main ()
{
  printf pov=2*(i1*i2+i2*i3+i3*i1);, pow (2.0, 2.0, 6.0, 3.0) );
  
  return 0;
  }
  
 /* Output: 7 ^ 3 = 343.000000 4.73 ^ 12 = 125410439.217423  32.01 ^ 1.54 = 208.036691 */
 


 

 

 

Task 3

Spoiler

Write a program in which simple class implementation is used only for text printing and no value is returned.

The task should illustrate an example of using a simple class. In the class definition, write two functions with the names tekst1 and tekst2 and declare them as public. The functions themselves declare as void, which means they do not return any value. Use them to print cout. In the main program, make the object by specifying the name of the class and the object name. In our case, it is the Ispis t, where is the is object. The object functions are called by specifying the name of the object, the point, and the function name (for example, t.text1 () ).

My solution

Spoiler

#include <iomanip>
#include <iostream>
using namespace std;

int main( int argc, char* argv[] )
  {void tekst1()
  cout << "Ovo je prvi tekst!  "<<endl;
  void tekst2()
  cout << "Ovo je drugi tekst!  "<<end2;
   return 0;
  }
  


int main( int argc, char* argv[] )
  {
  cout << "The name used to start the program: " << argv[ 0 ]
       << "\nArguments are:\n";
  for (int n = 1; n < argc; n++)
    cout << setw( 2 ) << n << ": " << argv[ n ] << '\n';
  return 0;
  }

  

 

 

 

The task text may have some grammar errors, I'm really sorry about that i tried my best in translating.

Thanks for reading!

Have a good day!

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Evellence said:

I don't really know  much about programming

 

Have you tried actually attending the professor's courses?

 

Random questions on "solution 1":

  • Why does it have three (!) "main()" methods? Which one, you think, will be called?
  • Who told you to use "using namespace std;"? You really never, ever shouldn't - especially not so late in the code.
  • Have you even tried to compile it before sending it to anyone and/or publishing it here? Because it most certainly won't. (Missing semicolon, to say the least. Your compiler would tell you.)

Also, the "KomplBr" class is partially "declared" but not "implemented".

Sorry, but there is no sane way to help you out without mainly doing all the work for you. "Sasa Petrovic", oh dear.

 

I haven't even looked at the other two "solutions" and I better won't, either.

edit: I did. Now I'll need to ram a needle into my eyes. Thank you.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Erik Sieghart said:

I honestly thought the above responses sounded pretty harsh until I actually looked at your code.

It doesn't even look like you've tried. It looks like you waited until the last minute, copied random code from stack overflow, and then sent it off to the professor. Your code doesn't even compile, which frankly confuses me, maybe because you don't know how to compile your code?

We're not unwilling to help, but we're not willing to do your homework for you, or help you pass a class that you seem to have every intention of failing.

 

1 hour ago, edward30 said:

Be thankful that your friend has gone away. He was not helping you.

 

Erik is right. Personally, I'm willing to help someone who really wants to learn. It doesn't look like you're serious about becoming a programmer. 

 

Your program starts on your main() function. You should only have one of those. And you should KNOW that you should only have one main(). It is hard to believe you could be serious about this work. You clearly have not read your textbook.

Here is my story. I've studied mechanical engineering, failed it sadly, exams were not hard, but professors were, when one of them starts to hate you on 1 class, you will surely fail on other 6 that hes teaching. Wont blame them for everything, it was my fault too, i made so much mistakes.

 

So i started college about programming, got accepted in second year but i still had to do exams for 1st year. In this short time that i had, i finished everything from first year and half of my second year. Programming in 1st year was about HTML, CSS and Jquery, which by now i know really well. But in second year is C++, which i really don't know and i'm trying to learn it. Math was my main problem, i finished it so i started focusing on programming again. 

 

I'm not asking you guys to finish my homework, i'm asking you to help me to learn it. I don't want to finish this and just forget all about it, i want to learn it well and find a job about programming. And no, i'm not waiting last second. I have 2.5 more months time to finish this.

 

So i'm really sorry for misunderstanding. And for this long post also, but what i'm trying to say is that i'm not that guy who says "hey please finish my homework" (which i kinda sounded like that). I want to learn it myself, and to make it my job later on in my life. And from my personal experience fixing code like this is best way to learn

 

 

TL;DR: I don't expect you to finish my job, nor would anyone want that, i need to do it myself so i can learn it, but i need just a little help 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Dat Guy said:

Now I'll need to ram a needle into my eyes. Thank you.

I tried, i really did. I know i did a really bad job about it. 

And i'm sorry. 

 

But thank you for your response. 

Link to comment
Share on other sites

Link to post
Share on other sites

Throw everything away.

 

Write a working "Hello World" in C++, preferably with a class which does the output job or something (I know nobody would do that in actual applications, but at least you'll have a fairly simple class).

 

Compile.

 

Compile again.

 

Compile until it stops throwing a temper tantrum because you failed to write valid C++.

 

Then try again. Should be doable in 2.5 months.

 

http://www.learncpp.com

Note that I didn't check the quality of that one thoroughly, but it might be an adequate starting point.

 

(Sent from the eye shop.)

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

^ Right now, this will do nothing (visible).

 

edit: Also, chances are that <math.h>, while perfectly working in C, won't be a valid C++ header. @edward30 got it almost right though.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

I noticed that @Evellence uses both <cmath> and <math.h> within one "solution" which makes no sense at all. Concerning your own example @edward30: not everything that compiles should be advised. :D

 

Technically, math.h does not work well with C++.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, edward30 said:

It's just C with classes, right

 

Luckily, wrong.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

@edward30 Thats more that enough to get me started. 

@edward30, @Dat Guy i really appreciate the help, kinda thought that you would flame me more like before. I don't know anything about c++ as you can see. But hey everyone has to start from somewhere. I just started on more complex stuff :D

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...
On 8/10/2017 at 4:21 PM, Evellence said:

...

I would suggest you to search for 'thenewboston' on youtube and find C++ courses. You'll find plenty of them and that guy will teach you everything you need. 2.5months is more than enough to learn and apply what you learned to your tasks.

 || CPU: Intel i5-8600K || Cooler: CoolerMaster Hyper 212X || Motherboard: Gigabyte Z370 HD3P || GPU: Gigabyte GTX 1050ti OC Windforce 4GB || Memory: 16GB Crucial DDR4 3000mhz || HDD: WD Black 500GB + Seagate Barracuda 2TB || SSD: Samsung 980 1TB || PSU: Corsair VS550 || Case: nJoy Ice Cage || Fans: Segotep Halo Ring RGB ||Monitor: 2x Dell 27" P2717H IPS Full HD || Second Monitor/TV: LG 49UJ620V UHD || Mouse: Logitech G502 || Keyboard: Logitech G810 + Royal Kludge RK84 || Speakers: Philips SPA-5300 subw + Arylic 2.1 + DIY Bookshelves w/ Dayton Audio || Headphones: HyperX Cloud Flight S ||

 

TO BE UPGRADED:

>> Headphones << >> Keyboard << >> HDD << >> Mouse << >> PC Case << >> Memory(another stick) << >> Graphics Card << 

 

Link to comment
Share on other sites

Link to post
Share on other sites

As @R3ep3r said, go for 'thenewboston' to learn C++. I too have opted for thenewboston recently and guess what? From having no knowledge of programming before and now I am learning OOP which is the most important aspect of any program.

 

Here is a link to a youtube video for c++ C++ comeplete tutorial

 

If you really really want to learn programming, you 'need' to go with different sources. It is going to be difficult and very time consuming, but this is what it is like to learn programming.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×