C language if else uotput question logic unable to understand - if-statement

#include <stdio.h>
void main()
{
int x = 5;
if (x < 1);
printf("Hello");
}
how it output hello
i expected error but there was no eroor

Because you have ';' after the "(x<1)"
Remove that semicolon and it will work.

Related

C++ Expected unqualified-id when trying to write a function

I wrote this code and was just trying to continue writing a function called register. I choose to set it to void because i am not certain if i am going to return anything at the moment. The Expected unqualified-id seems to show up when you put a semicolon in the wrong place, but i cant see that i make that error here..
I tried to comment out the menu.h include and all the three functions i get from the header file. No difference.
#include <iostream>
#include <string>
#include "menu.h"
//From the menu.h header file
int mainMenu();
int customerMenu();
int librarianMenu();
void register() { //Here the error appears.
}
int main () {
int runFlag = 1;
while(runFlag == 1) {
int mainMenuChoice = mainMenu();
if (mainMenuChoice == 1) {
customerMenu();
}
else if(mainMenuChoice == 2) {
librarianMenu();
}
}
}

exepted identifier or '(' before '{' token error while compiling

This might be a very stupid problem but i can't find a way to solve it.
i'm getting this error message while trying to compile this program:
Exercise1.c:28.1:exepted identifier or '(' before '{' token.
Can you please explain what am i doing wrong? Thanks.
#include <stdio.h>
#include <stdlib.h>
typedef struct II_node_struct
{int info;
struct II_node_struct *next;}II_node;
II_node* push(II_node* lis,int);
II_node* pop(II_node* lis);
int main()
{
II_node* pila;
II_node* corr=pila;
int x;
while(x>=0)
{scanf(" %d",&x);
if(x>0)
corr=push(corr,x);
if(x==0)
corr=pop(corr);}
while(corr->next!=NULL)
{printf("%d\n",corr->info);
corr=corr->next;}
return 0;
}
II_node* pop(II_node* lis);
{
II_node* risultato;
if(lis==NULL)
risultato=lis;
if(lis->next==NULL)
{risultato=lis;
free(lis);}
else
{risultato=lis->next;
free(lis);}
return risultato;
}
II_node* push(II_node* lis,int x)
{
II_node* new=malloc(sizeof(II_node));
if(lis==NULL)
{lis=new;
new->next=NULL;
new->info=x;}
else
{new->next=lis;
lis=new;}
return lis;
}

i keep get this "error C2059" C++

I am getting this error
error C2059: syntax error : 'if'
This is my code
// N.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
int main ()
{
int x,y,n,i,m;
std::cout<<"please enter a number";
i=0;
std::cin>>n;
for (x=1;x=n;x++)
for (y=1;y=n;y++)
if (x=y) m=x;
else;
while (x!=y) ;
do
{
if (x>y) x=x-y;
else y=y-x;
m=x;
}
if (m=1) i=i+1;
std::cout<<i;
return 0;
}
what is the problem ?
I am using microsoft visual studio 2008
The problem is that after the do { ... } the compiler is expecting a condition:
do
{
if (x>y) x=x-y;
else y=y-x;
m=x;
} while (condition);
In addition, your code seems to be not correct at all. For instance, your if (x=y) condition may be like this: if (x==y), and other...
You have errors in your for statements.
Use == for comparison, not = which is assignment.
Also, use < or <= as comparison. The == condition may be skipped over in loops.
A suggestion to help prevent these issues in the future: Use '{' and '}' with for, if, else and while.
For example:
for (x=1;x=n;x++)
{ // Insert this line.
for (y=1;y=n;y++)
{ // Insert this line.
if (x=y)
{
m=x;
}
else
{
;
}
} // End of for y
} // End of for x
The braces and indentation help spot errors during code reviews. Most coding styles demand the braces, even for single statements.
Also, use spaces to make code more readable. They don't impact the build time or code generation, but really help when reading the code:
for (x = 1; x <= n; x++)

C++: Error "declaration expected"

I have the following cpp file, and the error "Declaration expected" is thrown in this line, pointing at "for":
for (int i = 0; i < m_Floats.size(); ++i)
The entire code is:
#include "stdafx.h"
#include <vector>
#include "clsJenksBreaks.h"
using namespace std;
vector<float>m_Floats
vector<float>getJenksBreaks(const unsigned int uNumClass)
{
//std::sort(m_Floats, m_Floats + size, std::greater<float>());
float **mat1 = new float*[m_Floats.size()];
for (int i = 0; i < m_Floats.size(); ++i)
{
mat1[i] = new float[iNumClass];
}
for (unsigned long x=0;x<uNumClass+1;x++)
{
for (unsigned long y=0;y<m_Floats.size()+1,y++)
{
mat1[x][y]=0;
}
}
//I have commented out the other code that is in this function, but the error still exists.
}
Does anybody see where I went wrong?
There is no error on the line you indicate. The errors are:
missing semicolon at the end of line 7 (declaration of m_Floats).
missing declarations of iNumClass and uNumClass (presumably they're in the header you haven't shown us)
comma instead of semicolon on line 20, before the for-loop incrementor.
You're missing a semi-colon after the declaration of m_floats. Try:
vector<float>m_Floats;
Possible typo,
mat1[i] = new float[iNumClass];
should be
mat1[i] = new float[uNumClass];

need help on #include doesn't seem to be working

So I have a class called HPStack and I have to include it in my main class etc. However I get a "In File included from" error, what could be causing this?
Also my string objects also have errors I have have no idea why, the error is: "Unable to identifier string".
I'm new the C++ so any help would be appreciated, thanks in advance.
The error I am getting (I think) are these:
error: expected unqualified-id before "namespace"
error: expected `,' or `;' before "namespace"
error: expected namespace-name before ';' token
error: `<type error>' is not a namespace
Im not sure what I am missing but that isn't telling me much.
Here is my code: The class.h file.
#ifndef HPSTACK_H
#define HPSTACK_H
class HPStack {
public:
HPStack();
void push(double);
double pop();
double peek();
private:
double register_[4];
}
#endif
The class.cpp file.
#include "HPStack.h"
#include <cstdlib>
HPStack::HPStack() : register_{}{
}
double HPStack::push(double x) {
for (int i = 2; i >= 0; i--) {
if (isdigit(register_[i])) {
register_[i] = register_[i + 1];
}
register_[0] = x;
}
}
double HPStack::pop() {
return register_[0];
for (int i = 0; i < 3; i++) {
register_[i] = register_[i + 1];
}
}
double HPStack::peek() {
return register_[0];
}
And my main file:
#include "HPStack.h"
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
HPStack stack;
string line;
while (getline(cin, line)) {
stringstream expression(line);
string token;
while (expression >> token) {
if (isdigit(token[0])) {
stack.push(atof(token.data()));
} else if (token == "+") {
double x = stack.pop();
double y = stack.pop();
double z = (y + x);
stack.push(z);
}
}
cout << stack.peek();
}
The error is, I'm guessing, because of this line:
double register_[4] = {};
You can not initialize class members when declaring them.
If your compiler is new enough to support C++11 features, you can use an initializer list with the constructor:
HPStack::HPStack()
: register_{}
{
}
Otherwise you have to initialize the array manually in the constructor.
And as I noted in a comment, using register_ - 2 makes no sense as it returns a pointer so the index variable i will be way beyond the end of the array.
And using register_ - 1 as the condition in the pop loop makes even less sense, as it will always be non-zero and therefore always true and the loop will loop forever.
You're missing the ; at the end of the class definition:
class HPStack {
...
}; // <== This semicolon is required