How to convert a sentence to morse code using if statements - fortran

This is a sample of my program. I'm including else if statements for all the letters in the alphabet. Also the 10 before continue is on columns 4 and 5.
The first error I'm getting is saying Invalid radix specifier "or" at (2) for typeless constant at (1), with 2 being the O in OR and 1 being the first quotation in "A", "B", etc.
The second error is saying the same thing but with the 1 being at the second quotation in "a", "b", etc.
And my third and final error is saying Invalid form for ELSE IF statement at (^) with the carrot being at the end of each statement.
I'm assuming these have to do with the fact that I'm just using the wrong syntax for If, ELSE IF, and DO; but searching the internet hasn't helped me find the correct syntax.
program p1
integer j
integer sentenceSize
character sentence*72
print *, "Enter the size of the sentence you are converting: "
read *, sentenceSize
print *, "Enter the sentence you would like to convert: "
read(*,'(A)') sentence
DO 10, sentenceSize
IF (sentence(i:i) == "A" OR "a")
print *, ".-"
ELSE IF (sentence(i:i) == "B" OR "b")
print *, "-..."
ELSE IF (sentence(i:i) == 'C' OR 'c')
print *, "-.-."
ELSE IF (sentence(i:i) == "D" OR "d")
print *, "-.."
print *, " "
10 continue

There are many syntax errors in your program . Your DO loop is missing i=1. You need to put THEN after the IF and ELSE IF statements. A logic OR is .OR. Each OR'd item must be an expression (x == a .OR. y == c) not (x == a .OR. c). You need to terminate the IF-ELSE-THEN block with and END IF. And your program must end with END.
program p1
integer j
integer sentenceSize
character sentence*72
print *, "Enter the size of the sentence you are converting: "
read *, sentenceSize
print *, "Enter the sentence you would like to convert: "
read(*,'(A)') sentence
DO 10 i=1, sentenceSize
IF (sentence(i:i) == "A" .OR. sentence(i:i) == "a") THEN
print *, ".-"
ELSE IF (sentence(i:i) == "B" .OR. sentence(i:i) == "b") THEN
print *, "-..."
ELSE IF (sentence(i:i) == 'C' .OR. sentence(i:i) == 'c') THEN
print *, "-.-."
ELSE IF (sentence(i:i) == "D" .OR. sentence(i:i) == "d") THEN
print *, "-.."
END IF
print *, " "
10 continue
END

Related

C++ Blackjack code only going to first if statement

I'm trying to code a blackjack game and everything is going smoothly so far but for this bit. No matter what I input into hitStand it always goes to the first if statement and "hits". I would like for if "h" is inputted it "Hits" and if "s" is inputted it "Stands" and, if there is an invalid input, it will tell the user to try again.
I'm still fairly new to C++, so some help would be appreciated.
while (repeat == 0)
{
char hitStand;
cout << "Would you like to HIT or STAND [H/S]";
cin >> hitStand;
if (hitStand = "H" || "h")
{
PcardNew = rand() % 13 + 1;
cout << endl;
cout << "Your new card is: " << PcardNew << endl;
if (PcardNew > 10)
{
PcardNew = 10;
}
playerTotal = playerTotal + PcardNew;
cout << "Your new total is: " << playerTotal << endl;
}
else if (hitStand = "S" || "s")
{
break;
}
else
{
cout << "Please enter a valid imput [H/S]" << endl;
}
}
There are (at least) three errors in the single if (hitStand = "H" || "h") line!
First, the = operator is an assignment, not a comparison; to test for the equality of two operands, you need the == operator.
Second, the "H" and "h" constants are string literals - that is, multi-character, null-terminated strings of characters. Use single quotes for single characters (thus, 'H' and 'h').
Third, you can't compare one object with two others like that with a logical or (||) operator. You need to make two separate comparisons and then or the results of each:
So, use this:
if (hitStand == 'H' || hitStand == 'h')
{
//...
And similarly for your second test:
else if (hitStand == 'S' || hitStand == 's')
{
//...
That is because your condition in if statement is always true. Since "h" is in or (||).
Instead use:
if (hitStand == 'H' || hitStand == 'h')
and
else if (hitStand == 'S' || hitStand =='s')

Counting syllables in a word C++

I have created a program to count the syllables in a word inputted by the user. The user is to enter any amount of words followed by the enter key until the (#) key is entered, then the program is to display the words in a table followed by the syllable count for each word.
I am having issues with the "silent e" portion of my program.
if (word_length - 1 == 'e')
{
vowels = vowels - 1;
It seems as though it is not able to pick up the last letter in the string of words. I have tried to move some of the if statements around to see if that helps and to better identify where the problem lies, and from what I have noticed, as stated earlier i believe it to be with the silent e portion of the code.
It is difficult to find even the smallest errors in my code, so I am asking for another set of eyes to gaze over my code.
Any help will be greatly appreciated.
Also, I have yet to complete the formatting of my results table, so please over look that.
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
string word_1 = "create list";
int vowels = 0;
int word_length; // better to set .length() into variable to eliminate warnings
int words_to_print = 0; // this will count how many words to print to use in for loop later
/*
vector <variable type> name_of_vector[size of vector];
creating vectors
leaving them empty for now
*/
vector <string> words_saved;
vector <int> number_of_syllables_saved;
cout << "Enter 4 words from the English dictionary, to determine the amount of syllables each word has." << endl;
cout << "Please enter [#] when finished, to create a list." << endl;
cin >> word_1;
while (word_1 != "#") // as long as user doesnt enter # you can enter a word and
{ // have it run thru the syllable logic
word_length = word_1.length();
words_to_print++;
words_saved.push_back(word_1);
// ^ this saves the word into the next availabe index of vector for strings.
for (int i = 0; i < word_length ; i++) // length is a variable now instead of function syntax this
{ // eliminates the <: signed/usnsigned mismatch warning below
if ((word_1[i] == 'a') || (word_1[i] == 'e') || (word_1[i] == 'i') || (word_1[i] == 'o') || (word_1[i] == 'u') || (word_1[i] == 'y'))
{
vowels = vowels + 1;
if ((word_1[i + 1] == 'a') || (word_1[i + 1] == 'e') || (word_1[i + 1] == 'i') || (word_1[i + 1] == 'o') || (word_1[i + 1] == 'u') || (word_1[i + 1] == 'y'))
{
vowels = vowels - 1;
if (word_length - 1 == 'e')
{
vowels = vowels - 1;
if (vowels == 0)
{
vowels = vowels + 1;
}
}
}
}
}
number_of_syllables_saved.push_back(vowels);
//^ this puts number of syllables into vector of ints
vowels = 0; // this resets the amounts so it can count vowels of next word and not stack from previous word
cin >> word_1; // this will reset the word and controls loop to print out chart if # is entered
}
// use a for loop to print out all the words
cout << endl << endl << endl;
cout << "Word: " << setw(30) << "Syllables: " << endl;
for (int x = 0; x < words_to_print; x++)
{
cout << words_saved[x] << setw(20) << number_of_syllables_saved[x] << endl;
}
//system("pause");
return 0;
}
You are comparing the character 'e' to the integer word_length - 1 instead of comparing it to the last character of your string as you intended.
You should replace if (word_length - 1 == 'e') with if (word_1[word_length - 1] == 'e').
Your nested if-statements don't get evaluated at the anticipated index.
Say the input is "state".
At i = 0, word_1[i] = 's', doesn't pass the first if statement.
At i = 1, word_1[i] = 't', doesn't pass the first if statement.
At i = 2, word_1[i] = 'a', vowels becomes 1. The program does not proceed further down the nested if statements since word_1[i+1] is 't'.
At i = 3, word_1[i] = 't', doesn't pass the first if statement.
At i = 4, word_1[i] = 'e', vowels becomes 2. The program does not proceed further down the nested if statements since word_1[i+1] is garbage value.
As you can see, it never reaches the nested if-statements as you intended

How can I use an else if statement to compare letters and make it case insensitive?

So I have this block of code here:
string answer1;
answerQues1:
cout << "So, A, B, C or D?";
cin >> answer1;
if (answer1 == "A" | answer1 == "a")
{
cout << "It's the right answer, you have " << char(156) << "100!" << endl;
PlaySound(TEXT("C:/Users/user/Downloads/Millionaire/£100correct.wav"), NULL, SND_FILENAME | SND_ASYNC );
Sleep(2000);
}
else if (answer1 >= "E" | answer1 >= "e") //FIX THIS, IT'S ONLY ACCEPTING CAPITALS NOT LOWERCASES.
{
cout << "Whoops, you've entered an incorrect option! Remember, only A, B, C or D are acceptable." << endl;
answer1.erase();
goto answerQues1;
}
else
{
cout << "I'm very sorry, it's an early exit for you - it's the wrong answer! You leave with nothing!" << endl;
PlaySound(TEXT("C:/Users/user/Downloads/Millionaire/£100and£64000lose.wav"), NULL, SND_FILENAME | SND_ASYNC);
Sleep (4000);
PlaySound(TEXT("C:/Users/user/Downloads/Millionaire/gameOver.wav"), NULL, SND_FILENAME | SND_ASYNC);
cout << "Thanks for playing! Press any key to exit the game" << endl;
system("pause");
return 0;
}
The else if statement checks if the answer is greater or equal to the letter E, since only A, B, C or D are acceptable. It then displays a message telling the user, deletes the value of answer1, and then goes back to the input section, essentially making an loop. However, when I enter an incorrect option such as E and immediately enter a acceptable one such as B it does not accept B as a wrong answer and instead of going to the else statement, it goes to the else if ONLY IF THE ANSWER IS LOWERCASE. But if I typed B in capitals, it treats it as wrong and goes to the else statement as it should. Is there anyway I can fix this so it will detect upper and lowercase letters? Many thanks.
The problem is lower case letters are greater than upper case letters. See the ASCII table: http://www.asciitable.com/. You probably want:
else if ((answer1 >= "E" && answer1 <= "Z") || answer1 >= "e")
First, logical OR is ||, not |.
Second, take a look at an ASCII table and you'll see that 'e'>'E', so for answer="b" your check answer>"E" is true. You should better check that the value in an allowed range, so also give an end value.
Also: If you are only accepting one character as a choice, then make answer a char and not a string.
So a better solution would be
char answer;
if(answer == 'A' || answer == 'a')
...
if(!(('A' <= answer && answer <= 'D') || ('a' <= answer && answer <= 'd'))
...
You might also consider using the toupper/tolower functions on answer before checking the value.
And if you want it really safe, then keep in mind that ASCII is not the only encoding and not every character encoding assures that 'A'+1=='B' and 'B'+1=='C' and so on (EBCDIC for example is another character encoding where 'A'-'I' are in order, then there is a gap and than come 'J'-'R'). OK, I know, ASCII is the standard encoding, but it's not the worst idea to know the possible pitfalls :).
All strings are in some order, like "0" < "A" < "B" < "C" < "D" < "E" < "X" ... < "a" < "b" < "c" < "d" < "e" < "x" and so on.
All the lowercase letters come after the letter "E", that's why your test goes wrong. Test first for the acceptable answers, that is "A" and "a" as you did, then "B", "b", ... "d", and everything else is unacceptable.
If you are only going to accept A,B,C or D then why not accept the input as a char and convert it to a lower character. Admittedly, you can also do so with string - one char at a time.
Anyways, here's a sample for char:
char answer1;
std::cout << "So, A, B, C or D?";
std::cin >> answer1;
char lower = tolower(answer1);
std::cout <<" You entered: " << lower << std::endl;

how to exit if user enter q

When user presses Q it doesnt quit from the program.. What is wrong?? Help please
while (true)
{
//promt to user enter or quit
cout<<" Enter five digit number please or Q to quit \n";
cin>> buf; n = atoi (buf.c_str());
cin.ignore(1000,10);
if( n == 'q' || n == 'Q')
break;
a = n % 10;
b = n / 10000;
if ( ! a == b )
{
cout<< "This is not a palindrome \n";
continue;
}
// checking the palindrome
n = n % 10;
n = n / 100;
if ( a == b )
cout<<" This is palindrome\n";
else
cout<<" This is not a palindrome\n";
}
if you enter a 'q' character, the atoi function can not interpret this input as a number so a zero is returned.
Please refer to this link
http://www.cplusplus.com/reference/cstdlib/atoi/
The most important is the following paragraph:
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
In this case str == buf
If you enter q or Q to this program, it won't convert anything in atoi, and n will be 0, not 'q' or 'Q'. The program will exit if you enter the numeric value for 'q' or 'Q' in your character set, though (113 or 81, respectively).
Example:
Enter five digit number please or Q to quit
12345
This is not a palindrome
Enter five digit number please or Q to quit
12321
This is palindrome
Enter five digit number please or Q to quit
q
This is palindrome
Enter five digit number please or Q to quit
113
I'll leave the further debugging/fixing to you. Note - your program doesn't correctly check for palindromes.
As stated in previous answers, q will be converted to 0 with atoi(). So, basically, in order for the program to quit with a q or Q, use the atoi() conversion after
if( n == 'q' || n == 'Q')
break;
This way, n will still be q or Q when you check if the program should quit, and if not, you then convert n to an integer with atoi() and check if it is a palindrome.
Hope I could help
Try changing
if( n == 'q' || n == 'Q')
to
if( buf[0] == 'q' || buf[0] == 'Q')
This will access the first character in the input and compare it with the desired character 'Q' or 'q'

C++ do/while loop with two different conditions

I want the do while loop to check to see if the entered input is R OR P. I think it is checking for both and when I get to that part when I run, it pauses for a minute and then I get "CPU Limit Exceeded (core dumped). On another related note, am I in danger of breaking something?
/************************************************/
/* Name: servcode */
/* Description: Get service type */
/* Parameters: N/A */
/* Return Value: servcode */
/************************************************/
char servcode()
{
char servcode = 'a'; // Define variable for service code
char serviceyn = 'n'; // Define variable for user verify
int i = 1; // Define variable for sentinel loop
do {
cout << "\n" << "\n" << "Please enter your service code, [R]egular or [P]remium: " << "\n";
cin >> servcode;
while ((servcode != 'R', 'P') && (i < 3));
{
cout << "\n" << "Error - invalid service code, please try again.";
cout << "\n" << "Please enter your service code: ";
cin >> servcode;
i++;
if (i == 3)
{
cout << "\n" << "Too many invalid attempts, program terminating." << "\n"
<< "Have a nice day. " << "\n" << "\n";
exit (0);
} //end if
} //end while
cout << "\n" << "You entered: " << servcode << "\n"
<< "Is that correct? [y,n]";
cin >> serviceyn;
} while (serviceyn != 'y'); // end do/while loop
return servcode;
}
The correct syntax is:
while (servcode != 'R' && servcode != 'P' && i < 3)
Note the expanded comparison and the removal of the semicolon at the end:
(servcode != 'R', 'P') is valid C++ but doesn't do what you're expecting it to do;
the semicolon makes the statement into a loop with an empty body, so it continues executing forever since the loop condition never changes.
Change:
while ((servcode != 'R', 'P') && (i < 3));
to:
while ((servcode != 'R') && (servcode != 'P') && (i < 3))
Note the removal of an unwanted semicolon.
You need to do something like this:
while(something != 'a' && something != 'b')
You're using the comma operator, which discards the results of every expression except the last, so this:
while(something != 'a', 'b')
Will compare something with a, ignore the result, and use 'b' as the condition of the loop. 'b' is a non-zero value, so it's always true and the loop goes on forever (or until memory runs out, or something else stops it).
(servcode != 'R', 'P')
Should be:
(servcode != 'R') && (servcode != 'P')
Kudos to PaulR for correction. My brain is not in gear.
Let's break down this statement and see what it's doing. I've put in some extra punctuation that does not change the meaning of the statement.
while (((servcode != 'R'), ('P')) && (i < 3)) { };
The comma operator separates two different expressions and returns the value of the second one. The first part probably does what you expect, but the second part is just a literal character 'P' which is always true!
The semicolon at the end of the statement marks the body of what will be executed as the while loop. It's a do-nothing which does not change the value of servcode or i, so obviously once you enter the loop you will never leave it. This is a common mistake.