0

I`ve been programming in Java, and started learning C++ a few days ago. I was playing around with codes and got this :

# include <iostream>

using namespace std;

int main(){

	char str[100]; 
	char *pnt[100];
	
	for(int x = 0; x <= 100; x++){
		pnt[x] = str[x];
	}
	
	read();
	
	
}

void read(){
	cout<<"Enter your command :";
	cin.get(*pnt, 100);
	
	if(*pnt[0] == 'e' && *pnt[1] == 'c' && *pnt[2] == 'h' && *pnt[3] == 'o'){
		for(int i = 4; i <= 100; i++){
			cout<<*pnt[1];
		}
	}else {
		cout<<"There is no such command !";
		read();
	}
}

As I said, I`ve been programming in Java, and I`m totaly new to pointers. I tried everything I had in my mind, I just don`t know what else to do... I appreciate any kind of help, thanks!

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by thines01
0

What is your program supposed to do?

It looks like:
1) you want to copy 100 unused characters into str to pnt
2) you want to wait for the user to type the word "echo", then you will print it 100 times.

Is that right?

If so, you will need to pass the variable to the read() function.

If not, please explain.

Edited by thines01: typo

0

Basicly, yes, I started making kind`a command prompt, to practice a bit. So far I made the "echo" command. If the first character is 'e', the second - 'c', third - 'h', and fourth - 'o', the program prints the following characters, excluding 'echo'.

P.S. Sorry for my bad english...

0

Here's a much easier way to do that, if you don't want to compare character by character:

// Fujy.cpp : Defines the entry point for the console application.
//
#include <string>
#include <iostream>
using namespace std;

int main(int argc, char ** argv)
{
	string str = "";
	
	cout << "Enter your command" << endl;
	
	cin >> str;
	
	if("echo" != str)
	{
		cout << "unknown command";
	}
	else
	{
		for(int intLoop=0; intLoop < 100; intLoop++)
		{
			cout << str << endl;
		}
	}
	
	return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.