Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me fix the errors in my C++ code

Status
Not open for further replies.

ryusgnal

Advanced Member level 4
Joined
Oct 4, 2005
Messages
102
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,298
Location
Malaysia
Activity points
1,992
Hi I'm still new in C++ programming. can anybody tell me what is the problem with my code below and how to solve it? thank you very much.

#include <iostream.h>
#include <string.h>
char hello[6],ref;
main()
{
hello[6] = 'h','e','l','l','0','!';
ref='l';
if(strcmp(hello[2],ref)==0)
{cout<<"yes";}
else
{cout<<"no";}
return 0;
}
 

Re: Need help for C++

// hello.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>
#include <string.h>
char ref;

int main(int argc, char* argv[])
{
const char* hello[6] = {"h","e","l","l","0","!"};
ref='l';
if(strcmp(hello[2],&ref)==0)
{cout<<"yes";}
else
{cout<<"no";}
return 0;
}

you failed to cast the lookup table as an array of strings properly
a * is used to denote a location in memory
the & is simply data at address
so must be added to ref so strcmp can get the data at the char address and compare it to the data at the ref
so youll see if you compile the answer is yes
changing the value of ref changes the answer with respect

attached is the exe

further reading
**broken link removed**
 

    ryusgnal

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top