achieve_goals 0 Junior Poster in Training

Hey, I have improved my program and now its working if I enter @@@ at the end of line, but my requirement is
"You can stop your entering by inputting "@@@" in a new line as a sentinel."

How should I change my program to satisfy this requirement:

int main(int argc, char *argv[])
{
    Word listWord[200];
    string input;
    int len, tempcount, check = 0, listLen = 0;
    
    cout<<"Please enter your sentences, you can enter '@@@' by a new line to finish the input: ";
    getline(cin, input);
    
    istringstream in(input);
    string singleword;
    do
    {
          check = 0;
          in>>singleword;

		  if(singleword == "@@@")
				break;

          len = singleword.length();
          
		  for(int k=0; k<len; k++)
		  {
			  if(ispunct(singleword[k]))
			  {
				  if(singleword[k] != '-' && singleword[k] != '\'')
				  {
					  singleword.erase(k,1);
					  len = singleword.length();
					  k--;
				  }
			  }
		  }

		  len = singleword.length();

		  for(int i=0; i<len; i++)
                singleword[i] = tolower(singleword[i]);                  

          for(int j=0; j<listLen; j++)
          {
                  if(listWord[j].getName() == singleword)
                  {
                          tempcount = listWord[j].getCount();
                          listWord[j].setCount(tempcount+1);
                          check = 1;
                          break;
                  }
          }

          if(check == 0)
          {
                   listWord[listLen].setName(singleword);
                   listWord[listLen].setCount(1);
                   listLen++;
          }                    
    }while(in);

	sort(listWord, listLen);
	
	int distinctWord = 0;
	int totalWord = 0;
	for(int i=0; i<listLen; i++)
    {
            if(listWord[i].getCount() == 1)
                     distinctWord++;
			totalWord += listWord[i].getCount();
    }

    cout<<"\nTotal Number of words: "<<totalWord<<endl;
	cout<<"Total Number of distinct words: "<<distinctWord<<endl;
    
    cout<<"\nWord\t\tCount"<<endl;
    
	for(int i=0; i<listLen; i++)
            cout<<listWord[i].getName()<<"\t\t"<<listWord[i].getCount()<<endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks for helping.

achieve_goals 0 Junior Poster in Training

When I use the cin>>input instead of getline(cin, input), then it takes only the first word and store it in input.
But I want it to store the whole line.
For example if I input "Hi, hello, hello"
it counts "Hi" twice

achieve_goals 0 Junior Poster in Training

Hi,

I am trying to make a word counter, for that I have to input a string line untill '@@@' character is entered.
My problem is that when I enter "Hello hello" as my input it reads the last hello twice.. or this is what I think it does..
Right now I am just assuming that it takes input untill '\n' is entered.

Here is my code:

class Word
{
      public:
             void setName(string);
             void setCount(int);
             int getCount();
             string getName();
             
      private:
              string name;
              int count;
};
int main(int argc, char *argv[])
{
    Word listWord[200];
    string input;
    int len, tempcount, check = 0, listLen = 0;
    
    cout<<"Please enter your sentences, you can enter '@@@' by a new line to finish the input: ";
    getline(cin, input);
    
    istringstream in(input);
    string singleword;
    do
    {
          check = 0;
          in>>singleword;
          len = singleword.length();
          for(int i=0; i<len; i++)
          {
                singleword[i] = tolower(singleword[i]);                  
          }
          for(int j=0; j<listLen; j++)
          {
                  if(listWord[j].getName() == singleword)
                  {
                          tempcount = listWord[j].getCount();
                          listWord[j].setCount(tempcount+1);
                          check = 1;
                          break;
                  }
          }
          if(check == 0)
          {
                   listWord[listLen].setName(singleword);
                   listWord[listLen].setCount(1);
                   listLen++;
          }
          
          
    }while(in);
    

	int distinctWord = 0;
	int totalWord = 0;
	for(int i=0; i<listLen; i++)
    {
            if(listWord[i].getCount() == 1)
                     distinctWord++;
			totalWord += listWord[i].getCount();
    }

    cout<<"\nTotal Number of words: "<<totalWord<<endl;
	cout<<"Total Number of distinct words: "<<distinctWord<<endl;
    
    cout<<"\nWord\tCount"<<endl;
    for(int i=0; i<listLen; i++)
    {
            cout<<listWord[i].getName()<<"\t"<<listWord[i].getCount()<<endl;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void Word::setName(string newName)
{
     name = newName;
}

string Word::getName()
{
       return name;
}

void Word::setCount(int newCount)
{
     count = newCount;       
}

int Word::getCount()
{
    return count;    
}
achieve_goals 0 Junior Poster in Training

Ok, this is now what I have. Now it is getting the right data but the only issue is when I enter the address, of characters less than hundred, I have to press the enter key twice to go to the next option...

cout<<"Enter ID: ";
                       cin>>stdrec[i].id;
                       cin.ignore(200,'\n');

                       cout<<"Enter Firstname: ";
                       cin>>stdrec[i].firstname;
                       cin.ignore(200,'\n');

                       cout<<"Enter Lastname: ";
                       cin>>stdrec[i].lastname;
                       cin.ignore(200,'\n');

                       cout<<"Enter phoneno: ";
                       cin>>stdrec[i].phoneno;
                       cin.ignore(200,'\n');

                       cout<<"Enter address: ";
                       cin.getline(stdrec[i].address,100);
                       cin.ignore(200,'\n');

                       cout<<"Enter Date of Birth: \n";
                       cout<<"Month: ";
                       cin>>stdrec[i].dateofbirth.month;

                       cout<<"Day: ";
                       cin>>stdrec[i].dateofbirth.day;

                       cout<<"Year: ";
                       cin>>stdrec[i].dateofbirth.year;
                       cin.ignore(100,'\n');
                       i++;

Thanks for helping!

achieve_goals 0 Junior Poster in Training

Thanks for your reply.

I did change my code as you said and when I debug it its not concatenating the firstname with the Id but I still have the problem that it doesnt let me input lastname, phoneno.......
As the length of firstname is 20, I tried entering 19 character but it still waits for the 20th character...

achieve_goals 0 Junior Poster in Training

Hi,

I have to create a structure for student record and save their data, but I am having trouble in storing that data.

This is what I have:

struct student
{
       char id[7];
       char firstname[20], lastname[20];
       char phoneno[15];
       char address[100];
       dobtype dateofbirth;
};

int main(int argc, char *argv[])
{
    int choice;
    student stdrec[200];
    int i=0;
    cout<<"************** Address Book Menu *********** "<<entries<<" ****"<<endl;
         cout<<"1. Add an Entry"<<endl;
         cout<<"2. Display all Entries"<<endl;
         cout<<"3. Search an Entry"<<endl;
         cout<<"4. Delete an Entry"<<endl;
         cout<<"5. Exit\n>>"<<endl;
    
         cin>>choice;
    
         switch(choice)
         {
                  case 1:
                       cout<<"Enter ID: ";
                       cin>>stdrec[i].id;
                       cin.ignore(200,'\n');
                       cout<<"Enter Firstname: ";
                       cin.getline(stdrec[i].firstname,20);
                       cin.ignore(200,'\n');
                       cout<<"Enter Lastname: ";
                       cin.getline(stdrec[i].lastname,20);
                       cin.ignore(200,'\n');
                       cout<<"Enter phoneno: ";
                       cin.getline(stdrec[i].phoneno,15);
                       cin.ignore(200,'\n');
                       cout<<"Enter address: ";
                       cin.getline(stdrec[i].address,100);
                       cin.ignore(200,'\n');
                       cout<<"Enter Date of Birth: ";
                       cout<<"Month: ";
                       cin>>stdrec[i].dateofbirth.month;
                       cout<<"Day: ";
                       cin>>stdrec[i].dateofbirth.day;
                       cout<<"Year: ";
                       cin>>stdrec[i].dateofbirth.year;
                       break;
                       
                  case 2:
                       break;                       
         }
    }

The problem is when I enter 7 digits for Id it works, but when I enter 20 character for firstname, it then just displays the rest cout statements and the program terminates.
I tried to debug it and what I found was when I input firstname, first 9 characters are concatenated with the Id and other 9 characters are saved in firstname, after that I doesnt let the user enter anything...

achieve_goals 0 Junior Poster in Training

Hey, I GOT IT!!

THANKS A MILLION for your help.

There was an error in my coding!

achieve_goals 0 Junior Poster in Training

I have made a few changes, its working, but not giving me desired output. I think there is a problem with the comparison.
Here is my update code:

main()
{
    encrypt(plaintext, vigenere_table, keyword, ciphertext);
}
void encrypt(string plaintext, char vigenere_table[length][length], string keyword, string& ciphertext)
{
	string temp = keyword;
	int row, col;
     while(keyword.length() < plaintext.length())
     {
            keyword += temp;
     }
	 keyword = keyword.substr(0, 20);
     for(int i=0; i<20; i++)
	 {
		 for(int x=0; x<length; x++)
		 {
			 if(vigenere_table[x][1] == plaintext[i])
					row = x;
		 }
		 for(int y=0; y<length; y++)
		 {
			 if(vigenere_table[0][y] == keyword[i])
					col = y;
		 }
		 ciphertext += vigenere_table[row][col];
	 }
}
achieve_goals 0 Junior Poster in Training

This is what I am initializing the 2D array with.
I have a string of alphabets and I keep shifting left in order to get the following table. So every next line in the table is shifted left one character.


a b c d e f g h i j k l m n o p q r s t u v w x y z
b c d e f g h i j k l m n o p q r s t u v w x y z a
c d e f g h i j k l m n o p q r s t u v w x y z a b
d e f g h i j k l m n o p q r s t u v w x y z a b c
e f g h i j k l m n o p q r s t u v w x y z a b c d
f g h i j k l m n o p q r s t u v w x y z a b c d e
g h i j k l m n o p q r s t u v w x y z a b c d e f
h i j k l m n o p q r s t u v w x …

achieve_goals 0 Junior Poster in Training

And I am getting an error in my function call:

encrypt(plaintext, vigenere_table, keyword, ciphertext);

error C2664: 'encrypt' : cannot convert parameter 2 from 'std::string [27][27]' to 'std::string []'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

achieve_goals 0 Junior Poster in Training

Actually in my main I have declared 2D as:

string vigenere_table[length][length];

and I have another function for intiallizing it.

main()
{
    string alphabet = "abcdefghijklmnopqrstuvwxyz ";
    string vigenere_table[length][length];

    init_vigenere_table(alphabet, vigenere_table);
}

void init_vigenere_table(string& alphabet, string vigenere_table[length][length])
{
     for(int i = 0; i<length; i++)
     {
             vigenere_table[0][i] = alphabet[i];     
     }
     for(int i=1; i<length; i++)
     {
          shift(alphabet, i, vigenere_table);     
     }
     
}

Here I am generating a table of alphabets.

So I dont know what should I change in order to make it like your example of string s[].

achieve_goals 0 Junior Poster in Training

If I make those changes in my code, then how should I write my function call?

Right now, thats what I have:

encrypt(plaintext, vigenere_table, keyword, ciphertext);

Really appreciate your time and help.

achieve_goals 0 Junior Poster in Training

Cant I do anything about it, if I want to keep it string.
As I have used the variables plaintext and keyword in my main function as strings, and now if I change them into char array I am getting output with some weird symbols.

I didnt get you when you said that "You can overload the == operator(since you have already used the string class)" ??

achieve_goals 0 Junior Poster in Training

Hi,

As far as I know, we cant use a variable to declare the size of array.
In your code, you have:

char inc[n];  //where your n is integer type.

If you really want to do it that way, then declare a const of int type.

const int size = 8192;

main()
{
     char inc[size];
}
achieve_goals 0 Junior Poster in Training

Hi,

I am having trouble comparing characters from two strings.

This is what I have

void encrypt(string plaintext, string vigenere_table[length][length], string keyword, string& ciphertext)
{
	int row, col;
         for(int i=0; i<20; i++)
	 {
		 for(int x=0; x<length; x++)
		 {
		      if(vigenere_table[x][1] == plaintext[i])  //wont let me use ==
					row = x;
		 }
		 for(int y=0; y<length; y++)
		 {
		      if(vigenere_table[0][y] == keyword[i])   //wont let me use ==
					col = y;
		 }
		 ciphertext += vigenere_table[row][col];
	 }
}

Thanks in advance.

achieve_goals 0 Junior Poster in Training

Thanks for all your help

achieve_goals 0 Junior Poster in Training

Thank you so much for all your help.

achieve_goals 0 Junior Poster in Training

Can you please tell me how does the following statement works:

head->set_next(*temp)

If the set_next() function is something like this:

void set_next(Node temp)
	{
		temp.next = new Node;
	}

then does the statement: head->set_next(*temp) means that head.next points to a new node?

achieve_goals 0 Junior Poster in Training

My problem is not solved yet. I mean, I am still confused in creating a circular linked list. I am not comfortable in using set_next() function as I am not sure how will that work.

achieve_goals 0 Junior Poster in Training

Oh yes I got that error fixed (head->set_next(*temp))

I cant use head->next because next is a private member of Node class and I want to use it in CircularLinkedList class.

achieve_goals 0 Junior Poster in Training

Right now, I am NOT using link() function (that was what I had made initially). I am confused in CircularLinkedList.cpp; the set_number_people() function. In that function I want to use set_next() function (which is actually in Node.h) but dont really know how.

Thanks for replying.

achieve_goals 0 Junior Poster in Training

Hi,

I am confused in making circular linked list.
This is what I have.

CircularLinkedList.h

#ifndef CircularLinkedList_H
#define CircularLinkedList_H

class CircularLinkedList
{
public:
	void remove();
	void set_number_people(int);
	void print();
};
#endif

CircularLinkedList.cpp

#include "CircularLinkedList.h"
#include "Node.h"
#include <stdio.h>


void CircularLinkedList::set_number_people(int num_people)  //This is where I am confused
{	
	Node *head = new Node;
	int n = num_people;
	Node *temp = new Node;	
	*head->set_next(*temp);
	for(int x = 2; x < n; x++)
	{

	}
}

Node.h

#ifndef Node_H
#define Node_H
#include<stdio.h>

class Node
{
public:
	void set_data(int data)
	{
		this->data = data;
	}

	void set_next(Node temp)
	{
		temp.next = new Node;
	}

	int get_data(Node temp)
	{
		return temp->data;  //'->Node::data' : left operand has 'class' type, 
                                      //use '.'
	}

	Node get_next(Node temp)
	{
		return temp->next;
	}

private:
	int data;
	Node *next;
};
#endif

driver.cpp

#include<iostream>
#include "CircularLinkedList.h"

using namespace std;

int main()
{
	int people, count;
	cout<<"Enter number of people: ";
	cin>>people;
	cout<<"Enter number of counts: ";
	cin>>count;
	CircularLinkedList cclist;
	cclist.set_number_people(people);
	//cclist.print();
	return 0;
}

Initially I had made one method of making circular linked list in node class which is

Node link(int num_people)
	{
		int n = num_people;
		Node *head = new Node;
		head->data = 1;
		head->next = new Node;	
		head->next->data = 2;
		Node *temp = head->next;
		for(int x = 2; x <= n; x++)
		{
			temp->next = new Node;
			temp->next->data = x;
			temp = temp->next;		
			if(x == n)
			{
				temp = NULL;
			}
		}
		if(temp == NULL)
		{
			temp = head;
		} …
achieve_goals 0 Junior Poster in Training

Thanks for helping me out.
Is this what you meant:

int n = num_people;
		Node *start = new Node;
		start->next = new Node;	
		Node *p = start->next;
		for(int x = 2; x < n; x++)
		{
			p->next = new Node;
			p = p->next;		
			if(x == (n - 1))
			{
				p = NULL;
			}
		}
		if(p == NULL)
		{
			p = head;
		}
achieve_goals 0 Junior Poster in Training

Right now! I dont really know what is wrong but why dont you just try one thing, that is, declare an extra variable and at the end of your program write a cin statement. With all this stuff, you should get an output (of all three parts) and after that it will wait for an input.

I hope it helps ;)

achieve_goals 0 Junior Poster in Training

What is the output of this program? I mean does it display the number of words in the string (which is the last part of the program)?

achieve_goals 0 Junior Poster in Training

This is the code I came up with for circular linked list

achieve_goals 0 Junior Poster in Training

How can I edit my post, should I post it again with a different title.

achieve_goals 0 Junior Poster in Training

I will surely do that next time.
Thanks.

achieve_goals 0 Junior Poster in Training

I am trying to make a circular linked list.
I will appreciate if anyone can tell me how to make it better.

int n = num_people;
		Node *head = new Node;
		head->next = new Node;		
		Node *temp = head->next;
		for(int x = 2; x < n; x++)
		{
			temp->next = new Node;
			temp = temp->next;		
			if(x == (n - 1))
			{
				temp = NULL;
			}
		}
		if(temp == NULL)
		{
			temp = head;
		}

Thank you.

achieve_goals 0 Junior Poster in Training

Thank you amrith92 and abhimanipal for your help.

achieve_goals 0 Junior Poster in Training

I am sorry that i have not given you the details but i do have the .cpp file as well which is:

#include "CircularLinkedList.h"
#include "Node.h"
#include <stdio.h>


void set_number_people(int num_people)
{
    Node *head = new Node;  
    head->link(num_people); 
}

and also the Node.h

#ifndef Node_H
#define Node_H
#include<stdio.h>

class Node
{
public:
    void set_data(int data)
    {
        this->data = data;
    }
    void link(int num_people)
    {
        int n = num_people;
        Node *head = NULL;
        head->next = new Node;      
        Node *temp = head->next;
        for(int x = 2; x < n; x++)
        {
            temp->next = new Node;
            temp = temp->next;          
        }
    }

private:
    int data;
    Node *next;
};
#endif

I hope this makes sense.

achieve_goals 0 Junior Poster in Training

I do have the header and it is somewhat like this:

#ifndef CircularLinkedList_H
#define CircularLinkedList_H

class CircularLinkedList
{
public:
    void remove();
    void set_number_people(int);

};
#endif
achieve_goals 0 Junior Poster in Training

Thank you for your help.

Now I am having a different error.

error LNK2019: unresolved external symbol "public: void __thiscall CircularLinkedList::set_number_people(int)"

fatal error LNK1120: 1 unresolved externals

my driver.cpp is

#include<iostream>
#include "CircularLinkedList.h"

using namespace std;

void main()
{
    int people, count;
    cout<<"Enter number of people: ";
    cin>>people;
    cout<<"Enter number of counts: ";
    cin>>count;
    CircularLinkedList cclist;
    cclist.set_number_people(people);
}
achieve_goals 0 Junior Poster in Training

Sorry for the inconvenience, i got this error as well
Actually i was using java syntax that was I was using temp.link()
It should be temp->link().

achieve_goals 0 Junior Poster in Training

I got the errors, I had mistakenly comment a line which was

Node *head = NULL;

But I am still have error in the following statements

void set_number_people(int num_people)
{
    Node *head = NULL;
    Node *temp = new Node;  
    temp.link(num_people, head);

}


error C2228: left of '.link' must have class/struct/union (error in the last line)
achieve_goals 0 Junior Poster in Training

Hi,

I was working on Josephus problem, where I have to make a circular linked list and having errors in it. Will really appreciate if anyone can help me.

The following method is in Node Class:

void link(int num_people, Node* temp)
    {
        int n = num_people;

        [B]head->next = new Node;[/B]

        temp = head->next;
        for(int x = 2; x < n; x++)
        {
            temp->next = new Node;
            temp = temp->next;          
        }
    }

error C2065: 'head' : undeclared identifier (error in line which starts with head->next)
error C2227: left of '->next' must point to class/struct/union/generic type (error in line which starts with head->next)

error C2227: left of '->next' must point to class/struct/union/generic type(error in line which starts with temp = head->next)

Thanks in advance.

achieve_goals 0 Junior Poster in Training

Hi!

I am having trouble many a queue of Nodes.
I have a class of Queue (user defined - as I am not allowed to STL), with methonds as enqueue(), dequeue() and getFront().
And also I have a class of Node with data and frequency in it.
Now I have to make queue with nodes in it.

I am declaring it as

Queue Node_array[50];

but its not working.

Will appreciate early reply.

Thanks.

achieve_goals 0 Junior Poster in Training

No problem! :)

achieve_goals 0 Junior Poster in Training

I mean just put a break point in the second while loop and check what are the characters that you are getting and see if they are being changed to uppercase.

achieve_goals 0 Junior Poster in Training

Do you know how to use a debugger? it will be very easy for you to locate why its printing only the last character of your name.

achieve_goals 0 Junior Poster in Training

oh okay, thanks!

achieve_goals 0 Junior Poster in Training

Okay, correct me if I am wrong:

In your console, first you getting the character from file which you are printing on the screen, just making sure that you are getting it. You are getting the character on screen.

Then because of your second loop, let suppose in the first loop you got data which was in lower case, then you are getting the same character as the first one in your console.

Right??

achieve_goals 0 Junior Poster in Training

What do you get as output on console of that while loop, i mean is it in lowercase or uppercase? (after changing that statement to uppercase)

achieve_goals 0 Junior Poster in Training

In line 38 of your program, why have to used tolower() if you want it to be in uppercase?

achieve_goals 0 Junior Poster in Training

Thanks for reply.

Now I am using Node Node_array[50]; and its working, but now I have a different question.
How do we check it the node is null, like i want to check if Node_array is null and have to add some data to it.
How should I do that?

Really appreciate it.

achieve_goals 0 Junior Poster in Training

I have to make an array of Nodes.
I have #include "Node.h" as my header file
and then in main I am writing the following code

Node[] Node_array = new Node[];

but then I am getting errors referring to the above line:

error C2143: syntax error : missing ';' before '['
error C3409: empty attribute block is not allowed
error C2146: syntax error : missing ';' before identifier 'Node_array'
error C2065: 'Node_array' : undeclared identifier

Will appreciate early reply.

achieve_goals 0 Junior Poster in Training

Thanks for the help.

My file has the following data:

Marley was dead; to begin with.
Marley was as dead as a nail-door.
Marley was as dead as a nail-door.
Marley was dead.

And I have to take the prefix store it as key and its suffix as its value in hashtable, for example:

Key -Value
Marley was -dead;
was dead; -to
dead; to - begin
to begin -with
begin with -THE_END
Marley was -dead
and so on................. "-" is only for explaning the difference between key and value
and as we have multiple "Marley was" as key so its value will be a linked list.

Okay, I have written the following code for reading data from file and seperating the keys and value. Its working but plz do let me know if you think something is not good.

public void load_data(String fname) //throws IOException
{
    int counter;
    try
    {
    File file;
    file=new File(fname);

    if(!file.exists()&& file.length()<0)
        System.out.println("The specified file is not exist");

    else
    {
        FileInputStream finput=new FileInputStream(file);
        byte b;
        counter = 0;
        do
        {
            b=(byte)finput.read();             
            if(b == ' ' || b=='\r' || b =='\n' || b == -1)
            {
                counter += 1;
                if(counter >= 3)
                {
                    key =key_part1 + " " + key_part2;                       
                     Hash_key(key);
                     key = "";

                    key_part1 = key_part2;
                    key_part2 = value;
                    if(b == '\n')
                    {
                        value = "THE_END";
                    }
                    if(value == "THE_END")
                    {
                        counter = 0;
                        key_part1 = "";
                        key_part2 = "";
                    }                        
                    //Hash_value(value);
                    value = …
achieve_goals 0 Junior Poster in Training

Hey, need a little help, I am using the following code for readin data fom file:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class QuickFileRead {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner =
new Scanner(new File("c:/temp/text.txt")).useDelimiter("\\Z");
String contents = scanner.next();
System.out.println(contents);
scanner.close();
}
}

How can I change this to read data line by line or in a way that I can read words so that I can seperate the keys and their values.
My first two words will be the keys and the next work will be the value.
Thanks.

achieve_goals 0 Junior Poster in Training

I think that condition of not using JCL is only for hashtable or ArrayList, as our professor wants us to make that. So I guess I have to using JCL for reading files as there is not other way out!!
Thanks.

achieve_goals 0 Junior Poster in Training

Actually I have to make a hashtable. My data is in a datafile and I have to retrieve data from that without using Java Class Library, that means I cant use import java.io...
I have to take first two words to be the key and then the next word to be the value. Right now I am stuck in reading the data from file.

Thanks for your help.