Program Help - Read characters entered until a $ is entered

Hello, I wish to make a program that reads characters read from the keyboard, until a $ is entered.

Here is my code so far:

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
/*
	Write a program that reads characters from the keyboard until 
	a $ is typed.
	Have the program count the number of periods.
	Report the total at the end of the program. 
*/

#include <iostream>
using namespace std;

int main()
{
	char Typedletters, $;

	cout << "Start typing some characters:  ";
	cin >> Typedletters;

	switch(Typedletters){
		case 1: if(char Typedletters != $)
					cout << "Type some more characters in.";
		case 2: if(char Typedletters == $)
					cout << "You entered a $$$ Money sign!";
				
	}
	break;

					
	return 0;
}


This is the output I am aiming for.
Please enter your characters:  asdfasdfasdf

Continue to enter more characters: Morecharacters

Continue to enter more characters:  asdf$

You entered a $ sign!

Press any key to continue...


I would like the program to ask the first time for characters,
if a $ is NOT entered, I wish for the program to ask for characters to be entered again.

But when the character $ is entered, the program should stop.


Current errors:
1>------ Build started: Project: Unit 3 Module Program 1, Configuration: Debug Win32 ------
1>Compiling...
1>Unit 3 Module 1.cpp
1>l:\visual studio 2008\projects\unit 3 module program 1\unit 3 module program 1\unit 3 module 1.cpp(19) : error C2143: syntax error : missing ',' before '!='
1>l:\visual studio 2008\projects\unit 3 module program 1\unit 3 module program 1\unit 3 module 1.cpp(21) : error C2143: syntax error : missing ',' before '=='
1>l:\visual studio 2008\projects\unit 3 module program 1\unit 3 module program 1\unit 3 module 1.cpp(25) : error C2043: illegal break
1>Build log was saved at "file://l:\Visual Studio 2008\Projects\Unit 3 Module Program 1\Unit 3 Module Program 1\Debug\BuildLog.htm"
1>Unit 3 Module Program 1 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

First of all it is better do not use symbol $ as identifier though MS VC++ accepts it.
Secondly you did not initialize variable $. So the behavior of the program is undefined. You should write

char Typedletters, $ = '$';

1
2
3
4
5
6
Inside switch statement

		case 1: if(char Typedletters != $)
					cout << "Type some more characters in.";
		case 2: if(char Typedletters == $)
					cout << "You entered a $$$ Money sign!";


you shall remove type qualifier char and insert break statement after each if statement in the switch shown above.
Also you shall remove the break statement after the switch statement.
Last edited on
I forgot to add this to my program, in addition to stopping when a $ sign is entered, the program must also count the number of periods typed from the user.
Heres an easier way to solve this problem
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
#include <iostream>
using namespace std;

int main()
{
	char Typedletters;

	cout << "Start typing some characters:  ";
	loop:
	cin >> Typedletters;
                        if(Typedletters != '$')
                        {
					cout << "\nType some more characters in." << endl;
					goto loop;
                }
                    else if(Typedletters == '$')
                    {
					cout << "\nYou entered a $$$ Money sign!" << endl;;
                }
				


	system("PAUSE");				
	return 0;
}

the "loop:"
is like a marker in a program when you use the goto loop;
function it will send the program back to that point its very useful, and there are many other types of loops that you can check out here: http://www.youtube.com/watch?v=f9AqzVy-68U&feature=share&list=UUw9ZG29wGBK1LsB0-wdXPpA
Oh sorry forgot to show you haw to count periods heres the code:
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
#include <iostream>
using namespace std;

int main()
{
	char Typedletters;
    int nump=0;
   	
    loop:
	cout << "Start typing some characters:  ";
	cin >> Typedletters;
                if(Typedletters != '$')
                {
                if(Typedletters=='.')
                {
                 nump++;                     
                }
					goto loop;
                }
                    else if(Typedletters == '$')
                    {
					cout << "\nYou entered a $$$ Money sign!" << endl;
					cout << "You typed '.' " << nump << " times!" << endl;
                }
				


	system("PAUSE");				
	return 0;
}

in the first if statement i set up another if statement inside of it which says if the character variable Typedletters does not equal '$'(when comparing character variables always use ' ' around the character that is being looked for if it is not already declared a variable) and if Typedletters does equal '.' than add a number to the counter nump which is then cout at the end showing how many times the character '.' is typed in any further questions ask me
here are many other types of loops that you can check out here


There are a few types of loops. What you presented wasn't one of them. It was, however, a great example of something to avoid doing.
I do apologize but what should be avoided cuz i do not see what you are referencing
Spaghetti code. The arbitrary and indiscrete usage of goto.

That's why cuz.
i could have used a while loop but i thought a goto would be easier for a novice programmer like BloodMoney to understand im sorry that you cannot appreciate my teaching but if you dont like it help him yourself dont insult my work
i could have used a while loop


Yes.

but if you dont like it help him yourself dont insult my work


If you don't want people to comment on "your work" don't post it in a public forum.

good fight sir you win this time hah but seriously what did you not like about my work
cire wrote:
Spaghetti code. The arbitrary and indiscrete usage of goto.


Seriously.
ok goto is a nice easy function for novice programmers to understand
ok goto is a nice easy function for novice programmers to understand


Which is exactly why they shouldn't use it. The problem with goto is that it becomes easy to make spaghetti-code; an unreadable, unmaintainable, unfixable mess. To use goto without doing this on anything other than a piece of toy code requires experience, knowledge, design, testing.... essentially all the qualities novices don't have.

Novices have one good chance to learn how to use loops properly. It is extraordinarily difficult to relearn how to code a few years into the future because everything you learned is bloody awful. Learn it correctly now, at the start.
Now i see what your saying thank you ill try to be more aware of this next time

gsizzle10
Topic archived. No new replies allowed.