what to learn next in c++?

i everyone im looking for some recomendations on what i should learn next in c++. Ive been programming for a about a few months now and I know most of the basics like arrays, pointers , loops etc.. do you think i should learn data structures or algorithms next? or i need to learn some other things before learning algorithms? to get a better understanding of my skill i wrote that code that i wrote mysel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
using namespace std;
int n;
int arr[100];
int i;

 

int search(int arr[], int n, int val)

{
	for (int i= 0; i < n; i++)
	{

		if (arr[i] == val)
		{
			bool found = true;
			return true;
		}

		else
		{
			bool found = false;
			return false;


		}

	}

}

int main()
{

	int val;

	bool found;



	cout << " enter number of elements " << endl;

	cin >> n;


	for (i = 0; i < n; i++)
	{
		cout << "enter elelement" << i + 1 << " " << endl;
		cin >> arr[i];

	}

	cout << "enter element to search for" << endl;

	cin >> val;



	found = search(arr, n, val);


	if (found)

	{

		cout << "foundd";
	}

	else

	{

		cout << "not found";
	}
}

please let me know if you guys haveany recomendation or suggestions. Thanks everyone!
Last edited on
Why all the global variables?

Did you actually compile and test your program? I doubt it will truly produce your desired results. Looking at your program I think you need to continue learning the basics of the language. You really don't yet have a very good grasp of the basic building blocks of the language. Perhaps starting with something like: http://www.cplusplus.com/forum/articles/12974/

Perhaps you may consider actually learning to use some of the basic C++ features like vectors instead of arrays, C++ strings, to name a few.
thanks for the suggestions! i tried the programm and it gives me the correct results. I haven learned about the difference between global variables and local varibales yet. Ive done some of the excersies from the above link and they seem to work. If you have any other sources of where i can practice let me know. Thanks!
i tried the programm and it gives me the correct results.

Maybe if the match is always involving the first element of the array, but I doubt it will work when the matching element is in another element. In your search function that loop will only run one time so it can never check the entire array.

I haven learned about the difference between global variables and local varibales yet.

Then that seems like something you need to research and do something about.

Ive done some of the excersies from the above link and they seem to work.

Have you done them all? By yourself with no help? Have you had anyone review the programs you wrote for those exercises?
I have checed with multiple elements and involving different elements of the array and it always gives the correct result.
I think the loop just searches through the array once and if it finds the value entered by the user it returns true.



I did everything except for graduation and dungeon crawl. Im learning by myself so I dont have much help. But it took me multiple attempts to complete each assignment. here is one with the strings.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include<iostream>

#include <string>
using namespace std;


int main()

{
	
	string Name;


	cout << "enter first and last name" << endl;

	getline(cin, Name);

for (string::reverse_iterator rit=Name.rbegin(); rit!=Name.rend(); ++rit)
   cout << *rit<<endl;

	for (int i = 0; i < Name.length(); i++)
	{
		if (toupper(Name[i]) == 'A' || toupper(Name[i]) == 'E' ||
			toupper(Name[i]) == 'I' || toupper(Name[i]) == 'O' ||
			toupper(Name[i]) == 'U')
		{
			Name[i] = 'z';
		}


	}





	cout << "hi" << Name << endl;







	system("pause");

	
	
	return 0;
}



not sure if this is an efficent way of completing the problem.So if you can, please let me know what i can do to make it better. Thanks.
Last edited on
TheSmallGuy wrote:
You can further simplify your code with a switch statement.

It wouldn't be any simpler or more efficient, although it might look a little neater.

So if you can, please let me know what i can do to make it better.


Rather than a switch, I might change
1
2
3
4
5
6
7
8
9
10
11
	for (int i = 0; i < Name.length(); i++)
	{
		if (toupper(Name[i]) == 'A' || toupper(Name[i]) == 'E' ||
			toupper(Name[i]) == 'I' || toupper(Name[i]) == 'O' ||
			toupper(Name[i]) == 'U')
		{
			Name[i] = 'z';
		}


	}


to use a function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cctype>  // required for toupper
#include <string>

bool is_vowel(char ch)
{
    static const std::string vowels("AEIOU");

    return std::string::npos != vowels.find(std::toupper(ch));
}

int main() 
{
     // ...

    for (int i = 0; i < Name.length(); i++)
        if ( is_vowel(Name[i]) ) Name[i] = 'z';

     // ... 
I have checed with multiple elements and involving different elements of the array and it always gives the correct result.

Really, what about the following? Shouldn't it return true (found)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 enter number of elements 
5
enter elelement1 
1
enter elelement2 
3
enter elelement3 
8
enter elelement4 
88
enter elelement5 
5
enter element to search for
88
not found 


I think the loop just searches through the array once and if it finds the value entered by the user it returns true.

No, it looks at the first element, then returns either true of false, it never checks any other element.
Last edited on
yeah it returns false in the following example .. But I took your advice and got rid of all the unessasary global variables and changed the code to the following and now it works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;

bool search(int Arr[], int n, int VAL);

int main()
{
	int Arr[100], n, val;
	bool found;

	cout << "Enter number of elements you want to insert ";
	cin >> n;

	for (int i = 0; i<n; i++)
	{
		cout << "Enter element " << i + 1 << ":";
		cin >> Arr[i];
	}

	cout << "Enter the number you want to search ";
	cin >> val;

	found = search(Arr, n, val);

	if (found)
		cout << "\nItem found";
	else
		cout << "\nItem not found";


	return 0;
}

bool search(int Arr[], int n, int VAL)
{
	for (int I = 0; I<n; I++)
	{
		if (Arr[I] == VAL)
		
			return true;
	}
	return false;

I think you mean performance here. If you compare raw code (switch statement) with your solution (std::string), raw code still wins.

No, I meant your solution would not be any simpler (as you claimed) or more efficient than that of the OP.

The code I presented was meant to showcase a more flexible approach, not a more performant one (which is, perhaps, why I didn't make such a claim.)

Keep trolling away, there.
Topic archived. No new replies allowed.