Reputation: 59
new to C++ programming. Can anyone tell me what's wrong with my following code? The problem right now is that the if statement is ignoring any value you give it, and just goes to the section of code within the if statement.
There's also a problem with the code within the if statement itself. It doesn't take any input from the user. Any help would be really appreciated! Thanks.
#include <stdio.h>
#define SIZE 80
#include <iostream>
int main(void)
{
FILE * pFile;
int c; // variable to hold character input by user
char sentence[SIZE]; // create char array
int i = 0; // initialize counter i
int number;
std::cout << "Give a number. 1 for file. Anything else for standard.";
std::cin >> number;
std::cin.clear();
if(number=1)
{
printf("Enter sentence to append: ");
fgets(sentence,256,stdin);
pFile=fopen("mylog.txt","a");
fputs(sentence,pFile);
fclose(pFile);
}
else
{
// prompt user to enter line of text
puts("Enter a line of text:");
// use getchar to read each character
while ((i < SIZE-1) && (c = getchar()) != '\n') {
sentence[i++] = c;}
sentence[i]= '\0';
// terminate string
// use puts to display sentence
puts("\nThe line entered was:");
puts(sentence);
}
}
Upvotes: 0
Views: 56
Reputation: 4515
The =
is an assignment operator. You are just setting the value of number
to 1
. The ==
operator will compare the two values.
Upvotes: 2