Need some help

Pages: 12
So this is a program I wrote about a week ago and just today I decided to make it loop, but there's one problem with it that I don't understand - on the first go, everything is fine, but after it loops back to the beginning at least once, it skips the part where it asks me to type in the variable "sentence". I went through it with the debugger, and it goes through that line just fine, but doesn't ask me to type anything after it's looped back at least once. The same thing happened when I tried using a label at the beginning and used the "goto" command near the end.
Oh and there's one more thing, while I'm on the topic of this code, does anyone know how to put "" brackets as part of a string? Cause near the end where it says - "The sentence " << sentence - I want to put brackets around SENTENCE, but putting 1 double bracket on each side turns it into a string, and putting 2 double brackets on each side makes 2 empty 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
#include <iostream>
#include <string>
using namespace std;
int main()																					//A program, which counts the amount of letters selected in a word
{
	while (1 == 1)
	{
		string sentence = "";
		string sentence_question = "What's your sentence?";
		string letter_question = "What's the letter that you wish to count in your sentence?";
		char sel_ch = '0';	//Selected character
		int counter = 0;

		cout << endl << endl << sentence_question << endl << endl;
		getline(cin, sentence);

		cout << endl << endl << letter_question << endl << endl;
		cin >> sel_ch;

		for (int letter_spot = 0; letter_spot < sentence.size(); letter_spot++)			
		{																				
			char ch = sentence.at(letter_spot);
		
			if (sel_ch == ch)
			{
				counter++;
			}
		}

		cout << endl << "The sentence " << sentence << " has the letter " << sel_ch << " in it " << counter << " times." << endl << endl;
	}
	system("pause>nul");
	return 0;
}
Last edited on
1
2
cin.ignore(); // use this
cout << endl << "The sentence " << sentence << " has the letter " << sel_ch << " in it " << counter << " times." << endl << endl;


Thx, it works now, but how does cin.ignore work? I've never used that before...
Read here - http://www.cplusplus.com/forum/beginner/9148/

P.S you can always google stuff and find answers for simple questions.
Last edited on
Ok, thx, that solves the entire 1st part of my question, but do you know about the other part? I'll just copy it here:

Oh and there's one more thing, while I'm on the topic of this code, does anyone know how to put "" brackets as part of a string? Cause near the end where it says - "The sentence " << sentence - I want to put brackets around SENTENCE, but putting 1 double bracket on each side turns it into a string, and putting 2 double brackets on each side makes 2 empty strings.
Im pretty sure your question is simple, but I do not understand it. What do you want to do exactly? Give an example.
How do I put double brackets as part of a string? They're used for creating strings, but how do I put them inside a string?
Last edited on
Brackets? Whats this brackets, brackets can mean plenty of things. And you dont need brackets to create a string.

string x = "Hey there";

See, no brackets here.

Please think of a better way of explaining what you want to say, and or give an example.
Right where it says
 
cout << endl << "The sentence " << sentence << " has the letter " << sel_ch << " in it " << counter << " times." << endl << endl;


I want to put double brackets "" around

<< sentence <<
Last edited on
" " These are called quotes, not brackets.

If you want to put quotes around it, just put it at the end of the first string, and at the beginning of the second one.

cout << "Hey there " << name << ", Welcome! << endl;

See what I mean? One at the end of Hey there, and one in the beginning of welcome.
Last edited on
Doesn't that turn the entire ", Welcome! << endl; into a string? I admit that I haven't been explaining myself very clearly, so I'll say it like this:

I want to put double quotes around my variable SENTENCE on the line:

cout << endl << "The sentence " << sentence << " has the letter " << sel_ch << " in it " << counter << " times." << endl << endl;
I want to put double quotes around my variable SENTENCE on the line:

1. You do not have a variable called SENTENCE, its called sentence.
2. I Yep I dunno what I was thinking there :D
3. Why would you want to do that?
I made it uppercase in the comment so it would stand out. I want to put it in double quotes cause I want to make it something like this:

For example, sentence = "something"
sel_ch = o

and the last line of code

 
cout << endl << "The sentence " << sentence << " has the letter " << sel_ch << " in it " << counter << " times." << endl << endl;


would look something like:

The sentence something has the letter o in it 1 times.

I want it to look like;

The sentence "something" has the letter "o" in it 1 time(s).


I suppose that sums up the entire question to "How can I put quotes inside a string?"
Last edited on
This shit was hard to figure out but, I did it at last.


1
2
string name = "Baloon";
cout << "Hey there \"" << name <<  "\" Hey there" << endl;


Play around with this and apply it to yours.

Last edited on
Ah, yes, thx, it finally works. I'll put the thread as solved and I've got a small off-topic question regarding classes and objects.

This is my 1st attempt at using classes and objects. The program itself doesn't do much, but the point was that I used classes and objects and I wanted to ask if this is an efficient use of classes & objects.

This is the source.cpp
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
#include "class.h"
using namespace std;

int main()
{
	beginning:
		int tHappy = 0;
		do
		{
			cout << endl << endl << "How happy is Tom on a scale of 1 to 100?: ";
			cin >> tHappy;
			if (tHappy > 100 || tHappy < 0)
			{
				firstRet("Wrong, try again!");
			}
		} while (tHappy > 100 || tHappy < 0);

		int tHunger = 0;
		do
		{
			cout << endl << endl << "How hungry is Tom on a scale of 1 to 100?: ";
			cin >> tHunger;
			if (tHunger > 100 || tHunger < 0)
			{
				secondRet("Wrong, try again!");
			}
		} while (tHunger > 100 || tHunger < 0);

		vTom obj(tHappy, tHunger);

		cout << endl << endl << obj.retStat() << endl << endl;
		repeat:
		string choiceUser = "";
		cout << "Repeat?: ";
		cin >> choiceUser;
		if (choiceUser == "yes")
		{
			goto beginning;
		}
		else if (choiceUser == "no")
		{
			return 0;
		}
		else
		{
			goto repeat;
		}


	system("pause>nul");
	return 0;
}


This is the header
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
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
#include <string>
using namespace std;

void firstRet(string oneRet)
{
	cout << endl << oneRet;
}

void secondRet(string twoRet)
{
	cout << endl << twoRet;
}

class vTom
{
private:
	int happiness;
	int hunger;
	string okOrNot;

public:
	vTom(int happy, int hungry)
	{
		setStat(happy, hungry);
	}

	void setStat(int happy, int hungry)
	{
		happiness = happy;
		hunger = hungry;
		getStat(happiness, hunger);
	}

	void getStat(int hap, int hun)
	{
		if (hap == 0 || hun == 100)
		{
			string state = "Tom is dead";
			set2Stat(state);
		}
		else if (hap == 69 && hun == 69)
		{
			string state = "Tom is aroused";
			set2Stat(state);
		}
		else
		{
			string state = "Tom is fine";
			set2Stat(state);
		}
	}

	void set2Stat(string state)
	{
		okOrNot = state;
	}

	string retStat()
	{
		return okOrNot;
	}

};

#endif 
Last edited on
There's a couple of problems here. Its not the best way of using classes no. You want the actual class and all the function declarations inside the header file, but you want all the function definitions in the cpp file. You dont want them both in the same file like you have it right now.

Here is a super simple example - https://www.youtube.com/watch?v=NTip15BHVZc&list=PLAE85DE8440AA6B83&index=15&ab_channel=thenewboston

Also, in the first code, you are using goto And you do not want to be using goto, its very bad programming practice, you should use loops instead.




Yes, I've noticed a lot of people saying that. Why is the goto command considered bad?
Becuase it makes the code unreadable. imagine a big program with a a bunch of gotos instead of loops or whatever. And you're reading through it. Lets say you're at line of code 150. Then all of a sudden a wild goto appears. And it says. goto repeat;

And now you're like... where the fuck is this repeat? Is it in this file? Is it in a different file? is it above me, is it below me, etc etc, and imagine that with a bunch of gotos. Stick to loops buddy. :p

I might haven gotten it 100% right, you can google it for more information.
So in other words this is better?

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 "class.h"
using namespace std;

int main()
{
	while (1 == 1)
	{
		int tHappy = 0;
		do
		{
			cout << endl << endl << "How happy is Metodi on a scale of 1 to 100?: ";
			cin >> tHappy;
			if (tHappy > 100 || tHappy < 0)
			{
				firstRet("Wrong, try again!");
			}
		} while (tHappy > 100 || tHappy < 0);

		int tHunger = 0;
		do
		{
			cout << endl << endl << "How hungry is Metodi on a scale of 1 to 100?: ";
			cin >> tHunger;
			if (tHunger > 100 || tHunger < 0)
			{
				secondRet("Wrong, try again!");
			}
		} while (tHunger > 100 || tHunger < 0);

		vMetodi obj(tHappy, tHunger);

		cout << endl << endl << obj.retStat() << endl << endl;
		string choiceUser = "";
		cout << "Repeat?: ";
		cin >> choiceUser;
		if (choiceUser == "yes")
		{
			
		}
		else if (choiceUser == "no")
		{
			break;
		}
		else
		{
			return 0;
		}

	}
	return 0;
}


But now I'm having trouble imagining how to make it return to choiceUser if you don't say either yes or no at the end.
But now I'm having trouble imagining how to make it return to choiceUser if you don't say either yes or no at the end.


As in, start at the very beginning of the loop where they have to enter firstRet etc?

Also yes, since its loops its much better, but I would never recommend using infinite loop conditions

while (1 == 1)
Pages: 12