getchar(), putchar(char), EOF - eof

#include<stdio.h>
int main(){
int c = getchar();
while(c != EOF){
putchar(c);
c = getchar();
}
}
In above code why does not the program terminates by itself after c becomes EOF?
Reference of the code > Book: K&R's The C Programming Language 2nd Edition,
Page: 18

getchar() will return EOF only if the end of file is reached. The ‘file’ here is the standard input itself.
This can be written as:
#include <stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF)
{
/*getchar() returns the the next available value which is in the input
buffer*/
putchar(c);
}
}

Related

Accounting for space between character search

Im having issues with my code. Its been a few years since I last wrote a even a basic program. My current issue is my program is supposed to count the number of characters entered by the user and return the count of a single char also entered by the user. Where am I going wrong at? I know it has to do with the space between words. Probably a noob mistake. Thanks in advance.
#include <iostream>
#include <string>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int numTimesAppear(char *mystring, char ch)//numTimes Appear function
{
int i;
int count=0;
for (i = 0; i < 100; i++)
{
if (mystring[i] == ch)
count++;
}
return count;
}
int main()
{
char str[100];
char ch;
int nooftimes;
cout << ("Enter a string of characters:") << endl;
cin >> str;
cout << ("Enter a character to count:") << endl;
cin >> ch;
cout << numTimesAppear(str, ch);
return 0;
}

Counter returns unexpected value

This my cpp code and I am confused because cnt returns x = 0 !
#include <iostream>
using namespace std;
int i;
int cnt () {
for (i = 0; i<15; i++) {
return i;
}
}
int main () {
int x;
x = cnt();
cout << x << endl;
}```
The return function stops every other instruction in the function from being executed and returns the chosen value to the function which called it. Your loop is executed just once.

Error in use of the pcap_findalldevs_ex Function in c++

Below this program to retrieve the list of adapters and print it on the screen :
#include <stdio.h>
#include <pcap.h>
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s/n", errbuf);
exit(1);
}
for(d= alldevs; d != NULL; d= d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)/n", d->description);
else
printf(" (No description available)/n");
}
if (i == 0)
{
printf("/nNo interfaces found! Make sure WinPcap is installed./n");
return 0;
}
pcap_freealldevs(alldevs);
}
It is compile But Give an Error :
Every body have These Errors, while using pcap.h, go to bellow link and download pcap, after it Install it :
http://www.winpcap.org/install/default.htm

Help to call overloaded insertion operator

I tried calling my overloaded inserter but it's not doing what it's supposed to do.
#include <iostream>
#include "SortedLinkedListInt.h"
#include <sstream>
using namespace std;
//CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(){
head = NULL;
size = 0;
}
//DESTRUCTOR
SortedLinkedListInt::~SortedLinkedListInt(){
while (head != NULL) {
Node* ptr = head;
head = head -> next;
delete ptr;
}
}
//COPY CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(const SortedLinkedListInt &obj){
for(Node* n = obj.head; n!=NULL; n=n->next)
add(n->data);
}
void SortedLinkedListInt::add(int newElement){
if (head == NULL){
head = new Node;
head->next = NULL;
head->data = (newElement);
}
else if(head->data > newElement){
Node* node1 = new Node;
node1->data = newElement;
node1->next = head;
head = node1;
}
else{
Node* node2;
for(node2=head; node2->next!= NULL; node2 = node2->next)
if(node2->next->data > newElement)
break;
Node* node = new Node;
node->next = (node2->next);
node->data = (newElement);
node2->next = (node);
++size;
}
}
bool SortedLinkedListInt::exists (int element){
for (Node* n = head; n != NULL; n = n -> next) // how to write n.getElement() in c++
if(element == n->data) //analogous to compareTo (java)
return true;
return false;
}
void SortedLinkedListInt::toString(){
for (Node* n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
}
void SortedLinkedListInt::operator <<(const int &sub){
add(sub);
for (Node* n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
}
The function is at the bottom of the above header file. Below is the main.cpp
#include <iostream>
#include "SortedLinkedListInt.h"
#include <cstdio>
using namespace std;
int main(){
SortedLinkedListInt *sll = new SortedLinkedListInt();
//SortedLinkedList <int> *sll = new SortedLinkedList<int>;
/*SortedLinkedList<int> sll2 = *sll;*/
sll->add(5);
sll->add(1);
sll->add(3);
sll->add(9);
sll->add(2);
sll->add(5);
cout << 5;
cout << 3;
//sll->toString();
int n = 4;
printf("%d does%s exist in list.\n", n, sll->exists(n) ? "": " not");
system("PAUSE");
}
cout << 5 or any number wont call the overloaded insertion operator. I wanted to have it do the same function as sll->(5). So instead of using sll->(x) all that will be done is cout << x;
I am not sure what you are trying to achieve but
cout << 5
calls the insertion operator of cout standard stream. If you want to call your own insertion operator at least left part of the statement must be your class.
I hope this helps.

Substring in Mips

How to get a substring of a string in Mips?
Just get a cross compiler, code it in C and get the output assembly. You can use the -S option if using gcc.
For example:
root#:~/stackoverflow# cat strstr.c
#include <string.h>
/*
* Find the first occurrence of find in s.
*/
char *
strstr(const char *s, const char *find)
{
char c, sc;
size_t len;
if ((c = *find++) != 0) {
len = strlen(find);
do {
do {
if ((sc = *s++) == 0)
return (NULL);
} while (sc != c);
} while (strncmp(s, find, len) != 0);
s--;
}
return (s);
}
root#:~/stackoverflow# gcc -S -mrnames
strstr.c -o strstr.s
strstr.c: In function `strstr':
strstr.c:23: warning: return discards qualifiers from pointer target type
root#:~/stackoverflow#