Desperate need of help

So I really need help in cpp. I've been taking this course for a few weeks but I am still very confused and havent quite got the hang of it. I need a step by step explanation..

Write a program to do the following things (remember to follow the coding standards)
o Prompt the user to enter two integers – store them in integer variables (num1 and
num2)
o Ask the user to enter 2 words – store them in string variables (string1 and string2)

For a pretty basic problem like this, I would check out the site's tutorial.
http://www.cplusplus.com/doc/tutorial/

Specifically, see the section on getting user input with cin:
http://www.cplusplus.com/doc/tutorial/basic_io/

Since your prompt is so simple, I'll show you a snippet of what to expect:
1
2
3
int num1, num2; // declare two int variables
std::cout << "Enter two numbers: "; // show text to prompt the user to type stuff
std::cin >> num1 >> num2; // put user input into the variables 


1
2
3
std::string str1, str2;  // declare two std::string variables
std::cout << "Enter two strings: "; // show text to prompt the user to type stuff
std::cin >> string1 >> string2; // put user input into the variables 
Last edited on
The first time I started learning cpp (not a long time ago hehe) I didn't get anything at all. When I saw a bunch of lines of code which had vectors (which is actually simpler than it seems) in it I thought that programming simply isn't for me.

I had only learnt the really really basic stuff like conditionals and data types. But you know what kept me up? Even though I knew I would never be a competitive programmer, I wanted to make a program that would help my math by generating math problems for me and I knew this was possible.

So what did I do? I opened Youtube and started learning ONLY what I needed to know to make this program. I used google and basically copy-pasted everything. But you know what this helped me a lot.


What I feel is that you're forcing down cpp in your system. That's not good. If you're not a reader then there are some really good tutorials on Youtube like from Derek Banas (but he's really really fast) and thenewboston.

Learn cpp for fun, for your own practical uses not to become a professional. And that is ultimately how you will master it. By solving problems (there are many sites which have problems so you can give them a look too).

So my suggestion:
Since it seems like you're not able to understand by reading, watch youtube tutorials (some youtubers demonstrate really well. I know thenewboston and Derek Banas but there are other creators as well pick whomever you liked.

Then when you have understood the basic stuff, start making programs and when you hit a road block google on how to implement it or even come to this forum to us! And when you have a good grasp you can start researching about the really complex stuff.

Also if you're not able to come up with applications then you can solve problems online from sites like hackerrank.

Or you could learn from sites such as this: (https://www.learncpp.com/). You can ask us for clarification or more details if you don't understand a particular part ^_^


____________________________________________________________________________

That being said, I'll give a brief explanation of the snippet Ganado gave.

int num1, num2 'int' is a datatype that tells the compiler what kind of data we are going to be using. 'int' is used for integers, 'char' likewise is used to store a character.

followed by int is the variable name. This is the variable which will hold an 'int'.
You need to mention the datatype of the variable so the compiler knows what this variable is going to hold (because c++ is very specific with such stuff).

so you could either declare the two variables with
1
2
int num1;
int num2;


or
int num1, num2 - here we use the ',' to tell the compiler that num2 is also part of int.


Now we need to tell the compiler to print something on the screen. So we use std::cout << "Enter two numbers: ";
Just remember that std::cout<< " text "; is used for printing text. Why 'std::'? Because of namespaces which you will learn later. '<<' tells the compiler that text is put to the screen.

Similarly we us std::cin to take in text from what the user types and put it in a variable.
'>>' tells the compiler that text is to be taken from what the user typed and put into variable.
num1 is where what is taken should be stores.

You could either do
cin >> num1;
cin >> num2;

or
cin >> num1 >> num2;

Using consecutive >> is just like the ',' it tells that both num1 and num2 are operated by 'cin' like how 'int' was responsible for num1 and num2.

And also remember that cin and cout belong to the headerfile <iostream>.
cin means console input
cout means console output
iostream means input output stream (knowing the meaning will help you remember)
To use cin and cout you must put #include<iostream> at the beginning of the code to tell the compiler that cin and cout are needed by the program.




Last edited on
Thank you so much that actually helped me out a lot! I've been in a very serious predicament whether if I should switch over majors because programming is just not very easy and I've been having a hard time really picking it up. My course professor goes very fast and most of my peers already have some deep knowledge about programming and have been doing for a longer time. I kinda can't do this for fun because my assignments and labs are graded so it's a do or don't kind of thing. Anyways thank you again for those words of encouragement.

QUESTION
so I wrote that part in it's says cin wasn't declared
what do i add for the #include part of the problem
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream> // for cin and cout
#include<string.h> // for string datatype

int main(){

   int num1, num2; // declare two int variables
   std::cout << "Enter two numbers: "; // show text to prompt the user to type stuff
   std::cin >> num1 >> num2; // put user input into the variables 

   std::string str1, str2;  // declare two std::string variables
   std::cout << "Enter two strings: "; // show text to prompt the user to type stuff
   std::cin >> string1 >> string2; // put user input into the variables 

   cin.get(); // waits for the user to hit a key and hence pauses the script
   return 0;
}


This is the entire code ^^
cin belongs to iostream so you should write #include<iostream> and likewise #include<string.h> for string.

Make sure you write std:: before cin and cout. By the way may I ask which compiler you are using to compile?


Oh by the way here's something useful, you could write using std::cout to make it so that you never have to write std:: in front of cout in the entire code again. Likewise for cin and string!

So if we were to do that in our above code, it would look like this (it's really helpful to save typing):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream> // for cin and cout
#include<string.h> // for string datatype


   using std::cout; // <--
   using std::cin; // <--
   using std::string; // <--
   // or just using std::cout, std::cin, std::string...   in one line

int main(){
  
   int num1, num2; // declare two int variables
   cout << "Enter two numbers: "; // show text to prompt the user to type stuff
   cin >> num1 >> num2; // put user input into the variables 

   string str1, str2;  // declare two std::string variables
   cout << "Enter two strings: "; // show text to prompt the user to type stuff
   cin >> string1 >> string2; // put user input into the variables 

   cin.get(); // waits for the user to hit a key and hence pauses the script
   return 0;
}


Just a handy trick.

And you can also write 'using namespace std;' if you want to make it so that you don't have to write std:: for anything at all! But this is highly depreciated by programmers because sometimes it might confuse the programmer.

But as long as you're using just the C++ Standard Library, then it can be useful especially for when you don't know where to use the std:: (but you can also use the compiler to warn you)

so the code would become:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream> // for cin and cout
#include<string.h> // for string datatype


   using namespace std; // <--

int main(){

   int num1, num2; // declare two int variables
   cout << "Enter two numbers: "; // show text to prompt the user to type stuff
   cin >> num1 >> num2; // put user input into the variables 

   string str1, str2;  // declare two std::string variables
   cout << "Enter two strings: "; // show text to prompt the user to type stuff
   cin >> string1 >> string2; // put user input into the variables 

   cin.get(); // waits for the user to hit a key and hence pauses the script
   return 0;
}


Yes there's a lot of things to cover in programming when you're learning it professionally so your professors are going to be really quick. Feel free to use this forum for any kind of question you have ^_^.

And start going through what your professors are doing. If you don't know a certain part like "string" then google it and lastly come here for clarifying! ^_^

Good luck!
Last edited on
One thing: <string.h> is not used for the std::string datatype. string.h is the C library for dealing with C-style strings (basically, null-terminated char arrays).

<string> contains the definition for std::string. It just so happened that <iostream> also internally includes <string>, but <string> should still be directly #included to use std::strings.
Last edited on
Topic archived. No new replies allowed.