Function as left operand - What do I do? - visual-c++

As much as I searched for answers, I can't quite understand any of them, so I decided to ask for direct help.
Basically, this program is supposed to read each digit of the number and then write it down normally. for now this is a 'skeleton' code as I am trying to get the main idea of how to program it further, but I am already stumbling upon errors.
I get an error in whichever line of switch function I use, yet I don't know how to solve it. I get an error error C2659: '=': function as left operand
#include <iostream>
#include <string>
using namespace std;
int number, digit;
int i;
string word[4];
void lowering();
void units();
void tens();
int main()
{
i = 4;
word[i];
cout << "Enter a number: ";
cin >> number;
cout << endl;
lowering();
units();
i--;
lowering();
tens();
for(int x = 4; x >=0; x--)
cout << word[x];
system("pause");
return 0;
}
void lowering()
{
digit = number % 10;
number = number / 10;
}
void units()
{
switch (digit)
{
case 1:
word[i].append = " one"; break;
case 2:
word[i].append = " two"; break;
case 3:
word[i].append = " three"; break;
case 4:
word[i].append = " four"; break;
case 5:
word[i].append = " five"; break;
case 6:
word[i].append = " six"; break;
case 7:
word[i].append = " seven"; break;
case 8:
word[i].append = " eight"; break;
case 9:
word[i].append = " nine"; break;
default:
word[i].append = "";
}
word[i].append = " ";
}
void tens()
{
switch (digit)
{
case 1:
word[i].append = " ten"; break;
case 2:
word[i].append = " twenty "; break;
case 3:
word[i].append = " thirty "; break;
case 4:
word[i].append = " fourty"; break;
case 5:
word[i].append = " fifty"; break;
case 6:
word[i].append = " sixty"; break;
case 7:
word[i].append = " seventy"; break;
case 8:
word[i].append = " eighty"; break;
case 9:
word[i].append = " ninety"; break;
default:
word[i].append = "";
}
word[i].append = " ";
}

In your switch cases, you should use this:
void units()
{
switch (digit)
{
case 1:
word[i].append(" one"); break;
case 2:
word[i].append(" two"); break;
case 3:
word[i].append(" three"); break;
case 4:
word[i].append(" four"); break;
case 5:
word[i].append(" five"); break;
case 6:
word[i].append(" six"); break;
case 7:
word[i].append(" seven"); break;
case 8:
word[i].append(" eight"); break;
case 9:
word[i].append(" nine"); break;
default:
word[i].append("");
}
word[i].append(" ");
}
Besides for better code quality, you should put the break; into a new line.

Related

my program does not continue compiling if I don't type any letter

I am solving the problem set1_ credit in cs50. the program woks fine, however after entering the credit card number, i need to enter any letter in the keyboard in order for the program to give me the answer.
here is my code
# include <cs50.h>
# include <stdio.h>
# include <math.h>
long credit(void);
int main(void)
{
long long num = credit();
long long num2 = num;
int c = log10(num), i, sum , sum1, sum2 = 0, digits[c], divid[c], remind[c], total[c], cards[c];
scanf("%d", &c);
for ( i = 0; i <= c ; i++)
{
digits[c-i] = num % 10;
num = num /10;
scanf("%d", &digits[c-i]);
}
if ( c % 2 != 0)
{
for ( i = 0 ; i <= c ; i++)
{
if ( i % 2 == 0 )
{
digits[i] = digits [i] * 2;
}
else
{
digits[i] = digits [i];
}
}
}
else
{
for ( i= 0 ; i <= c ; i++)
{
if ( i % 2 != 0)
{
digits [i] = digits[i] * 2;
}
else
{
digits[i] = digits[i];
}
}
}
for ( i = 0; i <= c; i++ )
{
remind[i] = digits[i] % 10;
}
//printf("\n");
for (i = 0; i <= c; i++)
{
divid[i] = digits[i] / 10;
}
for ( i = 0; i <= c; i++ )
{
total[i] = remind[i] + divid[i];
}
for ( i = 0; i <= c ; i++ )
{
sum = sum + total[i];
}
// recreate the card's number in a form of an array
for ( i = 0; i <= c ; i++)
{
cards[c-i] = num2 % 10;
num2 = num2 /10;
scanf("%d", &cards[c-i]);
}
// check the nature and the validity of a card
int cards1 = cards[1];
if (sum % 10 == 0 && cards[0] == 3)
{
if (c == 15 )
{
switch (cards [1])
{
case 4: printf("AMEX");
break;
case 7: printf("AMEX");
break;
}
}
//return 0;
printf("AMEX");
}
else if (cards[0] == 5 && sum % 10 == 0)
{
if (c == 16)
{
switch (cards [1])
{
case 1: printf("MASTERCARD");
break;
case 2: printf("MASTERCARD");
break;
case 3: printf("MASTERCARD");
break;
case 4: printf("MASTERCARD");
break;
case 5: printf("MASTERCARD");
break;
}
}
//return 0;
printf("MASTERCARD\n");
}
else if (cards[0] == 4 && sum % 10 == 0)
{
switch (c)
{
case 13: //printf("VISA\n");
break;
case 16: //printf("VISA\n");
break;
}
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
printf("\n");
}
// get number of digits of an integer
long credit(void)
{
long long n;
do
{
n = get_long_long("Number: ");
}
while (log10(n) < 12 || log10(n) > 16);
return n;
}
I would be very grateful if anyone could help me solve this issue.
thanks in advance.
From man scanf:
The scanf() function reads input from the standard input stream stdin
The program is waiting for keyborad input (stdin) at one of the several scanf commands.
NB This question description is misleading because the program does compile, it (seemingly) stops running until keyboard input.

My while loop does not work

i want to check the user input which cant larger than 2 and smaller than 1 as well as cant be char (because the input type is int) but my code seem not working.....anyone can help me out? I tried various way but seem i still get it
update: here is part the code , source is declared as int in previous part
Student stu;
List<Student> list;
char id[10];
string str;
int choice;
int source;
bool ask;
while(true){
switch(menu()) {
case 1:
{
system("cls");
if (ReadFile("student.txt", &list)) {
cout << "\nRead File Successfully!\n";
}
else
cout << "\nRead File Failed!\n";
system("pause");
break;
}
case 2:
{
system("cls");
if(list.empty()){
cout<<"\nThe list is empty!\n";
break;
}
cout<<"\nStudent id: ";
getline(cin,str);
while(!isdigit(str)){
cout<<"\nEnter [x] back to menu or re-enter the student id: ";
getline(cin,str);
if(str=="x"||str=="X"){
break;
}
}
strcpy_s(id,10,str.c_str());
if(DeleteRecord(&list, id)){
cout<<"\nStudent successfully deleted!\n";
}
else
cout<<"\nDelete student record failed!\n";
system("pause");
break;
}
case 3:
{
system("cls");
if(list.empty()){
cout<<"\nThe list is empty!\n";
break;
}
cout<<"\nStudent id: ";
getline(cin,str);
while(!isdigit(str)){
cout<<"\nEnter [x] back to menu or re-enter the student id: ";
getline(cin,str);
if(str=="x"||str=="X"){
break;
}
}
strcpy_s(id,10,str.c_str());
if(SearchStudent(&list, id, stu)){
cout<<"\nStudent record found!\n";
}
else
cout<<"\nStudent record not found!\n";
system("pause");
break;
}
case 4:
{
system("cls");
/*if(InsertResult("exam.txt",&list)) //Call InsertResult function
cout<<"*INSERT RESULT SUCCESSFULLY!"<<endl;
else
cout<<"*INSERT RESULT FAILED!"<<endl;
system("pause");
break;
}
case 5:
{
system("cls");
/*if(InsertSubject("subject.txt",&list)) //Call InsertSubject function
cout<<"*INSERT SUBJECT SUCCESSFULLY!"<<endl;
else
cout<<"*INSERT SUBJECT FAILED!"<<endl;*/
system("pause");
break;
}
case 6:
{
system("cls");
cout<<"\nWhere Do You Want To Display The Information?"<<endl;
cout<<"\n1.Screen."<<endl;
cout<<"\n2.File."<<endl;
cout<<endl;
cin>>source;
//check the input
while(!isdigit(source)||source<1||source>2)
{
cout<<"\nPlease Enter A Valid Number Of Source!"<<endl;
cin>>source;
cout<<endl;
}
cout<<"\nWhich Information Do You Want To Display?"<<endl;
cout<<"\n1.Student Information."<<endl;
cout<<"\n2.Student Information & Past Exam Result."<<endl;
cout<<"\n3.Student Information & Current Subject Taken."<<endl;
cout<<"\n4.Student Information & Past Exam Result & Current Subject Taken."<<endl;
cout<<endl;
cin>>choice;
//check the input
if(!isdigit(choice) || choice<1 || choice>2)
{
cout<<"\nPlease Enter A Valid Number Of Choice!"<<endl;
cin>>choice;
cout<<endl;
}
Display(&list,choice,source);
system("pause");
break;
}
case 7:
{
system("cls");
cout << "\nThank You For Using The Program!\n";
system("pause");
return 0;
}
}
}
cout << endl;
system("pause");
}
isdigit expects you to pass a character variable to it. The link I provided has an example:
/* isdigit example */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
char str[]="1776ad";
int year;
if (isdigit(str[0]))
{
year = atoi (str);
printf ("The year that followed %d was %d.\n",year,year+1);
}
return 0;
}
You don't need to use isdigit because you are already using a int variable. Thus, this is the code you need:
case 6:
{
system("cls");
cout<<"\nWhere Do You Want To Display The Information?"<<endl;
cout<<"\n1.Screen."<<endl;
cout<<"\n2.File."<<endl;
cout<<endl;
cin>>source;
//check the input
while (source < 1 || source > 2)
{
cout<<"\nPlease Enter A Valid Number Of Source!"<<endl;
cin>>source;
cout<<endl;
}
}
cant larger than 2
source > 2
and smaller than 1
guessing you mean "can't be smaller than 1" source < 1
demo:
#include <iostream>
#include <ctype.h>
using namespace std;
int main(){
char source; // assuming source is char!!
cin>>source;
while(source < '1' || source > '2' || (!isdigit(source)) )
{
cout<<"\nPlease Enter A Valid Number Of Source!"<<endl;
cin>>source;
cout<<endl;
}
return 0;
}
Consider changing the code like this
while(!isdigit(source) || source < '1' || source > '2'))
{
cout<<"\nPlease Enter A Valid Number Of Source!"<<endl;
cin>>source;
cout<<endl;
}
Try this do-while loop Conditions,
do{
cout<<"\nPlease Enter A Valid Number Of Source!"<<endl;
cin>>source;
cout<<endl;
}while(source<1 || source>2 || !isdigit(source));
Edited
char digit; // holds only one char or one digit (0 1 2 3 ...9)
int source;
cin>>digit;
source=digit-'0'; // char to int
do{
cout<<"\nPlease Enter A Valid Number Of Source!"<<endl;
cin>>source;
cout<<endl;
}while(source<1 || source>2 || !isdigit(digit));//digit is character
In isdigit(arg) function,pass char argument its working fine.

How would I get a while loop to terminate when the user inputs "no"

I have this while loop for a Blackjack Class:
while (playerSum < 21) {
System.out.println("Would you like another card?");
String input;
System.out.flush();
input = in.readLine();
if (input.equalsIgnoreCase("yes")){
int card3;
Random r3 = new Random();
card3 = r3.nextInt(11 - 2 + 1) + 2;
switch (card3) {
case 2: System.out.println("You were dealt a Two"); break;
case 3: System.out.println("You were dealt a Three"); break;
case 4: System.out.println("You were dealt a Four"); break;
case 5: System.out.println("You were dealt a Five"); break;
case 6: System.out.println("You were dealt a Six"); break;
case 7: System.out.println("You were dealt a Seven"); break;
case 8: System.out.println("You were dealt a Eight"); break;
case 9: System.out.println("You were dealt a Nine"); break;
case 10: System.out.println("You were dealt a Ten"); break;
case 11: System.out.println("You were dealt a Ace"); break;
}
playerSum += card3;
}
}
I want the loop to end when the user inputs a "no" when asked if he would like another card. Currently If i input anything but "yes" it will continue to ask `Would you like another card?'. I understand why this is happening, but I have no idea how to fix it. Thanks in advance.
Add an else to the end of the if-Statement and break out of the loop.
if (input.equalsIgnoreCase("yes")){
int card3;
Random r3 = new Random();
card3 = r3.nextInt(11 - 2 + 1) + 2;
switch (card3) {
case 2: System.out.println("You were dealt a Two"); break;
case 3: System.out.println("You were dealt a Three"); break;
case 4: System.out.println("You were dealt a Four"); break;
case 5: System.out.println("You were dealt a Five"); break;
case 6: System.out.println("You were dealt a Six"); break;
case 7: System.out.println("You were dealt a Seven"); break;
case 8: System.out.println("You were dealt a Eight"); break;
case 9: System.out.println("You were dealt a Nine"); break;
case 10: System.out.println("You were dealt a Ten"); break;
case 11: System.out.println("You were dealt a Ace"); break;
}
playerSum += card3;
} else {
break;
}
Note: This will break out of the loop when the user inputs anything different than "yes". If you want to break out only on no, use else if.
You could simply fix this by adding
break;
So it would look like
if (no) { break;}
Hope this helps.

ncurses nodelay for responsive arrow key feedback

I am trying to write integers to a file using ncurses and the keyboard arrows. I use nodelay so that I can write a 4 to the file while nothing is being pressed. ESC exits the program. The problem is that all I can ever write to the files is 4. ESC seems to work fine, so the switch is working. If I take out the nodelay the program works but 4 cannot ever be writen.
Thanks
#include <ncurses.h>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ofstream outFile;
char outputFilename[80];
sprintf(outputFilename, "files/file.%s",argv[1]);
outFile.open(outputFilename, ios::out);
int ch;
initscr();
nodelay(stdscr,TRUE);
raw();
keypad(stdscr, TRUE);
noecho();
refresh();
while(1){
ch = getch();
switch(ch)
{
case KEY_UP:
outFile << 0 << endl;
break;
case KEY_DOWN:
outFile << 1 << endl;
break;
case KEY_RIGHT:
outFile << 2 << endl;
break;
case KEY_LEFT:
outFile << 3 << endl;
break;
case ERR:
outFile << 4 << endl;
break;
case 27:
outFile.close();
endwin();
return 0;
break;
default:
break;
}
refresh();
}
}
I was able to fix this issue by checking to see if the last value of ch was ERR before writing 4 to the file. Not sure I understand completely... maybe a timing issue. New program:
#include <ncurses.h>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
ofstream outFile;
char outputFilename[80];
sprintf(outputFilename, "interactive/taker.%s",argv[1]);
outFile.open(outputFilename, ios::out);
int ch;
int ch_prev=0;
initscr();
nodelay(stdscr,TRUE);
raw();
keypad(stdscr, TRUE);
noecho();
while(1){
ch = getch();
switch(ch)
{
case KEY_UP:
outFile << 0 << endl;
break;
case KEY_DOWN:
outFile << 1 << endl;
break;
case KEY_RIGHT:
outFile << 2 << endl;
break;
case KEY_LEFT:
outFile << 3 << endl;
break;
case ERR:
if (ch_prev != ERR)
outFile << 4 << endl;
break;
case 27:
outFile.close();
endwin();
return 0;
break;
default:
break;
}
ch_prev=ch;
refresh();
}
}
Without the check, you will overlook the lines which are not 4's, since almost all of the return values are ERR's. Also - the program will be using a lot of CPU (doing nothing). You would get better results if you used timeout with a fairly short value (10-50 milliseconds) rather than nodelay.

when using while loop in the progam, the application is not exiting

I have created an application in visual studio 2012 c++. The idea is to read the data from the serial port and to change the color for the label accordingly.I have used while loop to change the color in a continuous manner. Now, the application is working perfectly. The problem am facing is the application not existing when i click on the exit button or 'x' button in the application.I can able to close only in task manager by clicking on end task. The following is code of my application. Please let me know is there any way to fix this problem
while(checkBox1->Checked)
{
if(this->serialPort1->IsOpen)
{
int b[4] = {0};
int *ptr1;
ptr1 = b;
//this->serialPort1->DiscardInBuffer();
for (int i=0; i<4; i++)
{
*ptr1 = this->serialPort1->ReadChar();
ptr1++;
}
int address;
address = ((b[0]-48)*10)+(b[1]-48);
System::Windows::Forms::Label ^ ptr;
switch (address)
{
case 1: ptr = label1;
break;
case 2: ptr = label2;
break;
case 3: ptr = label3;
break;
case 4: ptr = label4;
break;
case 5: ptr = label5;
break;
case 6: ptr = label6;
break;
case 7: ptr = label7;
break;
case 8: ptr = label8;
break;
default: //MessageBox::Show("Default Case");
break;
}
if(b[2]=='o')
{
ptr->BackColor = Color::Red;
ptr->Text="R";
}
else if(b[2]=='a')
{
ptr->Text=String::Empty;
ptr->BackColor = Color::Green;
}
else if(b[2]=='i')
{
ptr->Text=String::Empty;
ptr->BackColor = Color::Blue;
}
if ((b[3]-48)<3)
{
ptr->Text=String::Empty;
ptr->BackColor = Color::SaddleBrown;
//_sleep(5000);
}
If you do UI programming, you must use an event driven mechanism! In your case, you should register to the "DataReceived" event in order to get called if new data arrives from the port.
private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e)
{
array<unsigned char> ^data = gcnew array<unsigned char>(4);
serialPort1->Read(data, 0, 4);
// Do whatever you want...
}
In the event handler "checkBox1_CheckedChanged" you should just open or close the serial port!
private: System::Void checkBox1_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
{
if (checkBox1->Checked)
{
serialPort1->Open();
}
else
{
serialPort1->Close();
}
}
Also I suggest to set the "read theresehold" to 4...
serialPort1->ReceivedBytesThreshold = 4;

Resources